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