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
相关推荐
她说人狗殊途8 小时前
基于 vue-cli 创建
前端·javascript·vue.js
大家的林语冰9 小时前
Deno 2.8 正式发布,再次超越 Bun,史上最大的次版本升级诞生!
前端·javascript·node.js
吃好睡好便好9 小时前
创建魔方矩阵和单位矩阵
开发语言·人工智能·学习·线性代数·matlab·矩阵
影寂ldy9 小时前
C#数组的属性和方法(Clear / Copy / IndexOf )
开发语言·javascript·c#
i7i8i9com9 小时前
Hermes Agent 安装记录
开发语言·bash·hermes
Brave & Real10 小时前
小程序 const 在js中以及与同类的var和let之间的差异
javascript·微信小程序·小程序
小娄~~10 小时前
C语言卷子错题集
c语言·开发语言·数据结构
米丘10 小时前
React 19.x 的 lazy 与 Suspense
前端·javascript·react.js
一然明月11 小时前
qt基本用法
开发语言·qt
hef28811 小时前
Prism图形设计从入门到精通:外观设置、图层顺序与微调技巧
开发语言