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
相关推荐
csbysj20209 分钟前
Lua 面向对象编程
开发语言
冴羽1 小时前
2026 年 Web 前端开发的 8 个趋势!
前端·javascript·vue.js
左直拳2 小时前
将c++程序部署到docker
开发语言·c++·docker
崇山峻岭之间2 小时前
Matlab学习记录31
开发语言·学习·matlab
fengbizhe2 小时前
bootstrapTable转DataTables,并给有着tfoot的DataTables加滚动条
javascript·bootstrap
刘一说2 小时前
TypeScript 与 JavaScript:现代前端开发的双子星
javascript·ubuntu·typescript
你怎么知道我是队长2 小时前
C语言---输入和输出
c语言·开发语言
mmz12072 小时前
二分查找(c++)
开发语言·c++·算法
EndingCoder3 小时前
类的继承和多态
linux·运维·前端·javascript·ubuntu·typescript