1. continue、break和return的区别

循环遍历。

for(let i = 0; i < 5; i++){
console.log(i); // 0 1 2 3 4
}

使用continuebreakreturn

for(let i = 0; i < 5; i++){
if(i==3){
continue;
}
console.log(i); // 0 1 2 4
}
for(let i = 0; i < 5; i++){
if(i==3){
break;
}
console.log(i); // 0 1 2
}
for(let i = 0; i < 5; i++){
if(i==3){
return;
}
console.log(i); // 0 1 2
}

我们可以看到continue跳出当前循环(即进入下次循环),而break跳出整个循环(即不再执行之后的循环)。

这里return也是跳出循环,但其实returnbreak是有区别的。我们再看几个例子。

(function foo(){
for(let i = 0; i < 5; i++){
if(i==3){
continue;
}
console.log(i); // 0 1 2 4 hello
}
console.log('hello');
})();
(function foo(){
for(let i = 0; i < 5; i++){
if(i==3){
break;
}
console.log(i); // 0 1 2 hello
}
console.log('hello');
})();
(function foo(){
for(let i = 0; i < 5; i++){
if(i==3){
return;
}
console.log(i); // 0 1 2
}
console.log('hello');
})();

提示:(function foo(){ //... })()是立即执行函数。

我们看到使用return时,没有打印hello。因此,return除了跳出循环的作用外,还有跳出当前函数的作用。

最新文章

  1. wxWidgets编译安装gtk问题的解决办法
  2. ural 2065. Different Sums
  3. 如何利用java得到当前的时间和前一天的时间
  4. Linux-vmware tools安装与cdrom挂载
  5. 7.适配器模式(Adapter Pattern)
  6. ROC和AUC介绍以及如何计算AUC
  7. Spring Data与elasticsearch版本对应关系
  8. 对FMDB的封装JRDB
  9. 学习MQ(一) 感知
  10. 201771010126 王燕《面向对象程序设计(java)》第二周学习总结
  11. cf1088D Ehab and another another xor problem (构造)
  12. python绘图工具matplotlib在linux下安装和使用
  13. 集合框架-Set集合
  14. 关于在pycharm下提示ModuleNotFoundError: No module named &#39;XXX&#39; 的一种可能
  15. 分页sql写法【只用最新的】
  16. video兼容--可用
  17. 【概率论】条件概率 &amp; 全概率公式 &amp; 朴素贝叶斯公式
  18. Nginx安装负载均衡配置 fair check扩展
  19. 阿里历年经典Java面试题汇总,想进BAT你还不快收藏!
  20. cent os 6.5 配置vsftpd

热门文章

  1. Scala 基础(二):sbt介绍与构建Scala项目
  2. java 基本语法(十七)Lambda (四)构造器引用与数组引用
  3. java 数据结构(十):Collection子接口:Set接口
  4. 3dTiles 数据规范详解[4.1] b3dm瓦片二进制数据文件结构
  5. Python基础-类与对象
  6. 据说比Spring快44倍的web开发框架,不妨试试
  7. Eclipse默认快捷键说明
  8. Python如何向SQLServer存储二进制图片
  9. idea2020安装教程
  10. 细说JavaScript 导出 上万条Excel数据