返回停止循环吗?

发布于 2025-02-12 11:53:50 字数 299 浏览 6 评论 0原文

如果我有以下循环的


    for (var i = 0; i < SomeArrayOfObject.length; i++) {
    
      if (SomeArray[i].SomeValue === SomeCondition) {

         var SomeVar = SomeArray[i].SomeProperty;
         return SomeVar;
      }
    }

返回语句停止函数的执行?

If I have the following for loop


    for (var i = 0; i < SomeArrayOfObject.length; i++) {
    
      if (SomeArray[i].SomeValue === SomeCondition) {

         var SomeVar = SomeArray[i].SomeProperty;
         return SomeVar;
      }
    }

Does the return statement stop the function's execution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

失与倦" 2025-02-19 11:53:50

是的,只要函数的控制流量满足返回语句,函数总是会结束。

下面的示例演示了返回语句如何结束函数的执行。

function returnMe() {
  for (var i = 0; i < 5; i++) {
    if (i === 1) return i;
  }
}

console.log(returnMe());

注意:请参见其他答案关于的特殊情况 try - catch - 最后此答案>回调具有其自己的功能范围,因此它不会突破包含的函数。

Yes, functions always end whenever their control flow meets a return statement.

The following example demonstrates how return statements end a function’s execution.

function returnMe() {
  for (var i = 0; i < 5; i++) {
    if (i === 1) return i;
  }
}

console.log(returnMe());

Notes: See this other answer about the special case of trycatchfinally and this answer about how the forEach callback has its own function scope, so it will not break out of the containing function.

缺⑴份安定 2025-02-19 11:53:50

大多数情况下(包括此),返回将立即退出。但是,如果返回是在中尝试带有随附的最后 block block中的最后始终执行并可以“覆盖” 中返回尝试。

function foo() {
    try {
        for (var i = 0; i < 10; i++) {
            if (i % 3 == 0) {
                return i; // This executes once
            }
        }
    } finally {
        return 42; // But this still executes
    }
}

console.log(foo()); // Prints 42

In most cases (including this one), return will exit immediately. However, if the return is in a try block with an accompanying finally block, the finally always executes and can "override" the return in the try.

function foo() {
    try {
        for (var i = 0; i < 10; i++) {
            if (i % 3 == 0) {
                return i; // This executes once
            }
        }
    } finally {
        return 42; // But this still executes
    }
}

console.log(foo()); // Prints 42
遥远的她 2025-02-19 11:53:50

此代码将在第一次迭代之后退出循环的第一次迭代:

const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
  if (iterator.name == 2) {
    return;
  }
  console.log(iterator.name);// 1
}

以下代码将在条件下跳跃,并继续以循环的继续进行:

const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];

for (const iterator of objc) {
  if (iterator.name == 2) {
    continue;
  }
  console.log(iterator.name); // 1  , 3
}

This code will exit the loop after the first iteration in a for of loop:

const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
  if (iterator.name == 2) {
    return;
  }
  console.log(iterator.name);// 1
}

the below code will jump on the condition and continue on a for of loop:

const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];

for (const iterator of objc) {
  if (iterator.name == 2) {
    continue;
  }
  console.log(iterator.name); // 1  , 3
}
病毒体 2025-02-19 11:53:50

返回语句仅在函数内部时停止循环(即它同时终止循环和函数)。否则,您将获得此错误:

Uncaught SyntaxError: Illegal return statement(…)

要终止循环,您应该使用break

The return statement stops a loop only if it's inside the function (i.e. it terminates both the loop and the function). Otherwise, you will get this error:

Uncaught SyntaxError: Illegal return statement(…)

To terminate a loop you should use break.

怀念你的温柔 2025-02-19 11:53:50

是的,一旦执行返回语句,整个函数将在当时退出。

试想一下,如果不这样做并继续循环会发生什么,并每次执行该返回语句?当您考虑它时,它将返回值的含义无效。

Yes, once the return statement is executed, the entire function is exited at that very point.

Just imagine what would happen if it did not and continued looping, and executing that return statement each time? It would invalidate it's meaning of returning a value when you think about it.

再见回来 2025-02-19 11:53:50

答案是肯定的,如果您编写返回语句,则控件会立即返回到呼叫者方法。
除了最终块,在返回语句之后将执行。

最后,如果您返回最终阻止,也可以覆盖您已返回的值。
-finally-retrife

catch

链接: try - :

返回语句可用于从控制流块中分支
并退出方法

返回语句终止函数的执行,并且
返回控制功能。执行在
在通话后立即进行呼叫功能。

维基百科:

返回语句导致执行留下当前子例程
然后在代码的位置恢复
子例程被称为其返回地址。返回地址
被保存,通常在过程的呼叫堆栈中,作为该过程的一部分
进行子例程调用的操作。许多人的返回语句
语言允许函数指定要传递的返回值
到称为函数的代码。

The answer is yes, if you write return statement the controls goes back to to the caller method immediately.
With an exception of finally block, which gets executed after the return statement.

and finally can also override the value you have returned, if you return inside of finally block.
LINK: Try-catch-finally-return clarification

Return Statement definition as per:

Java Docs:

a return statement can be used to branch out of a control flow block
and exit the method

MSDN Documentation:

The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.

Wikipedia:

A return statement causes execution to leave the current subroutine
and resume at the point in the code immediately after where the
subroutine was called, known as its return address. The return address
is saved, usually on the process's call stack, as part of the
operation of making the subroutine call. Return statements in many
languages allow a function to specify a return value to be passed back
to the code that called the function.

吾性傲以野 2025-02-19 11:53:50

“返回”确实退出了该函数,但是如果要返回大量数据,则可以将其存储在数组中,然后将其返回,而不是试图在循环中返回每个数据1 x 1。

"return" does exit the function but if you want to return large sums of data, you can store it in an array and then return it instead of trying to returning each piece of data 1 by 1 in the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文