ES6 - break 语句



break 语句用于从一个结构中跳出。在循环中使用 break 会导致程序退出循环。以下是 break 语句的一个示例。

示例

var i = 1
while(i< = 10) {
   if (i % 5 == 0) {
      console.log("The first multiple of 5 between 1 and 10 is : "+i)
      break //exit the loop if the first multiple is found
   }
   i++
}

上面的代码打印出 1 到 10 之间第一个 5 的倍数。

如果发现一个数字可以被 5 整除,if 结构就会使用 break 语句强制控制流退出循环。执行上述代码后,将显示以下输出。

The first multiple of 5 between 1 and 10 is: 5
广告
© . All rights reserved.