JavaScript——循环语句

简介: • js中循环语句有:• for• while• do-while

1. 循环语句的介绍

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

  • for
  • while
  • do-while


2. for循环

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


3. while循环

var array = [1, 4, 5];        
var index = 0;
while (index < array.length) {
    result = array[index];
    alert(result);
    index++;
}

说明:

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


4. do-while循环

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
目录
相关文章
|
17天前
|
JavaScript 前端开发
JS循环for、for...of、for...in
本文介绍了JavaScript中不同的循环语句,包括传统的`for`循环、`for...of`循环用于遍历数组和类数组对象、`for...in`循环用于遍历对象的属性,并通过示例代码展示了它们的用法和区别。
33 6
JS循环for、for...of、for...in
|
1月前
|
JavaScript 前端开发
JavaScript基础知识-流程控制之while循环
这篇文章介绍了JavaScript中的while循环和do...while循环的基础知识,并通过一个实际案例演示了如何使用while循环计算投资增长到特定金额所需的年数。
37 2
JavaScript基础知识-流程控制之while循环
|
2月前
|
JavaScript 前端开发
JavaScript中有哪几种循环?他们的运用场景在哪?
JavaScript中有哪几种循环?他们的运用场景在哪?
|
2月前
|
JavaScript 前端开发 索引
js的循环中foreach、for in和for of的区别
js的循环中foreach、for in和for of的区别
114 0
|
15天前
|
前端开发 JavaScript
前端基础(八)_JavaScript循环(for循环、for-in循环、for-of循环、while、do-while 循环、break 与 continue)
本文介绍了JavaScript中的循环语句,包括for循环、for-in循环、for-of循环、while循环、do-while循环以及break和continue的使用。
37 1
前端基础(八)_JavaScript循环(for循环、for-in循环、for-of循环、while、do-while 循环、break 与 continue)
|
14天前
|
JavaScript 前端开发 索引
|
1月前
|
JavaScript 前端开发
JavaScript基础知识-forEach循环
关于JavaScript基础知识中forEach循环的介绍。
31 1
JavaScript基础知识-forEach循环
|
2天前
|
JavaScript
自动循环提交js
自动循环提交js
7 0
|
28天前
|
JavaScript
js 循环数组取值
js 循环数组取值
|
29天前
|
JavaScript 前端开发
JavaScript while 循环
JavaScript while 循环
13 3