在嵌套循环JS函数中,“返回"是否退出循环或整个函数? [英] In a nested-loop JS function, does 'return' exit the loop, or the whole function?

查看:103
本文介绍了在嵌套循环JS函数中,“返回"是否退出循环或整个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段无法使用的JavaScript(Euler Project,问题3),并且我将嵌套循环作为解决方案的一部分.如果满足条件,我想退出一个子循环,然后继续父循环.我应该使用return还是break来完成此任务?

I have a piece of JavaScript I can't get to work (Euler Project, problem 3), and I have nested loops as part of the solution. If a condition is met, I want to exit a child loop, and continue with the parent loop. Should I be using return or break to accomplish this?

我已在此处阅读答案:是否可以从Javascript中的嵌套循环中脱离出来?

I've read the answers here: Best way to break from nested loops in Javascript? but am still confused, especially on the answer that includes an anonymous function as part of the solution.

推荐答案

返回 始终退出当前功能.

return always exits the current function.

如果您需要退出循环,则应使用 break 会终止当前循环,标签或switch语句.

If you need to exit a loop you should be using break which terminates the current loop, label or switch statement.

这篇关于在嵌套循环JS函数中,“返回"是否退出循环或整个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆