JavaScript----循环语句

1. 循环语句的介绍

循环语句就是让一部分代码重复执行,javascript中常用的循环语句有:

  • for
  • while
  • do-while

2. for循环

javascript 复制代码
var array = [1, 4, 5];

for(var index = 0; index < array.length; index++){
    result = array[index];
    alert(result);
}

3. while循环

javascript 复制代码
var array = [1, 4, 5];        
var index = 0;

while (index < array.length) {
    result = array[index];
    alert(result);
    index++;
}

说明:

当条件成立的时候, while语句会循环执行

4. do-while循环

javascript 复制代码
var array = [1, 4, 5];
var index = 0;

do {
    result = array[index];
    alert(result);
    index++;
} while (index < array.length);

说明:

当条件不成立的时候do语句也会执行一次

5. 小结

  • js中循环语句有:
    • for
    • while
    • do-while
相关推荐
To_OC6 小时前
别再瞎写 React Router!7 个高频踩坑点一次性讲透
前端·javascript·react.js
BUG研究员_7 小时前
Runnable与LCEL
开发语言·人工智能·python
岭南灯火8 小时前
前端通用交互式几何编辑器的设计法则 3 - 数据对象的设计
前端·javascript·架构
岭南灯火8 小时前
前端通用交互式几何编辑器的设计法则 2 - 交互事件流
前端·javascript·架构
岭南灯火8 小时前
前端通用交互式几何编辑器的设计法则 1 - 入口的对象及其成员
前端·javascript·架构
满栀5859 小时前
Vue.js 接口封装最佳实践:从基础到高级
前端·javascript·vue.js
牛艺翔9 小时前
C++基础
开发语言·c++
键盘会跳舞9 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
kyriewen9 小时前
我踩了三次同一个坑才明白:React里"改了数据却不重新渲染"的真正原因
前端·javascript·react.js
美味蛋炒饭.9 小时前
Git 版本控制(下)
开发语言·git·学习·总结·后端开发