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
相关推荐
kyriewen6 小时前
程序员连夜带团队跑路,省了23万:这AI太贵,真的用不起了
前端·javascript·openai
辞旧 lekkk7 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode7 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19978 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk8 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite9 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
我叫黑大帅9 小时前
为什么需要 @types/react?解决“无法找到模块 react 的声明文件”报错
前端·javascript·面试
之歆9 小时前
DAY_21JavaScript 深度解析:数组(Array)与函数(Function)(一)
前端·javascript
Cyber4K10 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Le_ee10 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php