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
相关推荐
大闲在人5 小时前
C、C++区别还是蛮大的
c语言·开发语言·c++
光影少年5 小时前
浏览器渲染原理?
前端·javascript·前端框架
小白探索世界欧耶!~5 小时前
Vue2项目引入sortablejs实现表格行拖曳排序
前端·javascript·vue.js·经验分享·elementui·html·echarts
Cosmoshhhyyy6 小时前
《Effective Java》解读第39条:注解优先于命名模式
java·开发语言
清水白石0087 小时前
Python 纯函数编程:从理念到实战的完整指南
开发语言·python
掘根7 小时前
【C++STL】平衡二叉树(AVL树)
开发语言·数据结构·c++
叫我一声阿雷吧7 小时前
JS实现响应式导航栏(移动端汉堡菜单)|适配多端+无缝交互【附完整源码】
开发语言·javascript·交互
前路不黑暗@7 小时前
Java项目:Java脚手架项目的文件服务(八)
java·开发语言·spring boot·学习·spring cloud·docker·maven
毅炼7 小时前
Java 集合常见问题总结(3)
java·开发语言·后端