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
相关推荐
浅水壁虎12 小时前
vue基础(第四章 Pinia)
前端·javascript·vue.js
LiLiYuan.12 小时前
【字符串常量池】
java·开发语言·面试
瑞码空间12 小时前
Python爬虫进阶实战笔记
开发语言·python·计算机·python爬虫
16月6日-晴12 小时前
Java面向对象进阶—static
java·开发语言
爱跳舞的烤冷面12 小时前
自学嵌入式第16天(数据结构篇--链表进阶操作)
开发语言
张元清13 小时前
React useEventSource Hook:自带断线重连的 Server-Sent Events (2026)
javascript·react.js
持敬chijing13 小时前
PHP开发-环境搭建-安装phpstudy-vscode工具
开发语言·vscode·php
铁皮饭盒13 小时前
网页端, 6.5MB人脸识别模型, 谷歌框架, 又快又准
前端·javascript·后端
charlie11451419113 小时前
Cinux · 第一次跳进 Ring 3:用户态与特权隔离
开发语言·c++·操作系统·开源项目
自进化Agent智能体13 小时前
Hermes 工具与工具集——Hermes 的内置能力
javascript