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
相关推荐
LXS_3577 小时前
Day 18 C++提高 之 STL常用容器(string、vector、deque)
开发语言·c++·笔记·学习方法·改行学it
C_心欲无痕7 小时前
vue3 - 依赖注入(provide/inject)组件跨层级通信的优雅方案
前端·javascript·vue.js
王琦03187 小时前
Python 函数详解
开发语言·python
胡伯来了7 小时前
13. Python打包工具- setuptools
开发语言·python
小鸡吃米…7 小时前
Python 中的多层继承
开发语言·python
BD_Marathon7 小时前
Vue3_响应式数据的处理方式
前端·javascript·vue.js
deng-c-f8 小时前
Linux C/C++ 学习日记(53):原子操作(二):实现shared_ptr
开发语言·c++·学习
嚣张丶小麦兜8 小时前
Vue常用工具库
前端·javascript·vue.js
wanghowie8 小时前
01.07 Java基础篇|函数式编程与语言新特性总览
java·开发语言·面试