迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中退出 for 循环

作者:迹忆客 最近更新:2024/03/17 浏览次数:

for 循环重复执行代码语句,直到满足指定的条件。大多数情况下,我们需要退出循环并中断连续执行。

在本文中,我们将学习如何使用 JavaScript 停止和中断 for 循环的执行。


退出 JavaScript 中的 for 循环

我们通常使用 break返回;在 JavaScript 中停止 for 循环执行的关键字。我们可以在我们想要的条件下使用这些关键字。

例如,假设我们正在寻找数据数组中的特殊字符。在这种情况下,我们实现等于特殊字符的条件,并在循环遍历数组期间中断执行。


在 JavaScript 中使用 break 关键字退出 for 循环

在我们定义的条件下,可以在 for 循环的块/大括号中使用 break

代码:

<script>
//break out the execution of for loop if found string
let array = [1,2,3,'a',4,5,6]
for (i = 0; i < array.length; i++) {
  console.log("array value: "+array[i])
    // if the element is string
     if (typeof(array[i])=="string") {
       console.log(array[i]+" is string at index: "+i+", break the loop")
       break; // break the loop
     }
}
</script>

输出:

"array value: 1"
"array value: 2"
"array value: 3"
"array value: a"
"a is string at index: 3, break the loop"

在上面的代码中,我们在该数组上应用了一个 for 循环,直到该数组的长度。在循环内部,我们通过说明我们遍历的数组元素是否为字符串来检查条件。

如果条件在任何索引处变为,我们就定义了 break 语句,循环将停止。我们已经打印了循环终止日志以找出数组中的索引和元素字符串。


在 JavaScript 中使用 return 关键字退出 for 循环

我们通常使用 return 语句来停止编程中函数的执行。如果我们在函数体中实现 for 循环,我们可以使用 return 停止它的执行。

代码:

<script>
let array = [1,2,3,'a',4,5,6]
myFunction();
function myFunction(){
  for (i = 0; i < array.length; i++) {
  console.log("array value: "+array[i])
      // if the element is string
     if (typeof(array[i])=="string") {
      console.log(array[i]+" is string at index: "+i+",loop stoped using return")
       return; // stop the loop using return
    }
  }
}
</script>

输出:

"array value: 1"
"array value: 2"
"array value: 3"
"array value: a"
"a is string at index: 3,loop stoped using return"

我们通过使用 return 关键字实现了与第一个例子相同的结果。我们将 for 循环代码放在函数的主体中,并在 if 条件的主体中使用 return 语句。

return 语句将停止函数主体和循环执行。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便