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
相关推荐
apcipot_rain4 小时前
C++转Java日记(04)
java·开发语言·c++
Penge6664 小时前
《Go 后端开发者学 React:组件通信与插槽》
javascript
云和数据.ChenGuang5 小时前
LangGraph 异步 Node 的设计与写法实践
开发语言·人工智能·python·机器学习·php
Penge6666 小时前
《Go 后端开发者学 React:useState》
javascript
Penge6666 小时前
《Go 后端开发者学 React:数据渲染》
javascript
蜗牛一步步往上爬9 小时前
第十六章 C++语言在深度学习中的应用
开发语言·c++·深度学习
ikoala9 小时前
AI 写页面像说明书,修 Bug 像贴膏药?把这两句话写进 AGENTS.md
前端·javascript·ai编程
Penge6669 小时前
《Go 后端开发者学 TypeScript:ES Module了解》
javascript
Penge6669 小时前
《Go 后端开发者学 TypeScript:从零搭建与运行》
javascript
Penge66610 小时前
《Go 后端开发者学 TypeScript:keyof关键字》
javascript