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
相关推荐
Chloe.Zz2 分钟前
一篇入门Agent
开发语言·前端·javascript
余醉 | dtminer14 分钟前
3337 OTU×999置换:群落构建mBio 高分沉积物
开发语言·r语言·微生物学
白杨尚青14 分钟前
C++入门篇(十八):模板进阶——非类型参数、特化全家桶与分离编译(收官篇)
开发语言·c++·笔记·模板·偏特化·分离编译
silvamux_19 分钟前
4款新模型商务邮件翻译横评:0.98到0.46
android·java·javascript
qetfw38 分钟前
Bash 内置与外部命令:type、command 与 builtin 验证
linux·开发语言·bash
Dovis(誓平步青云)41 分钟前
车辆进入隧道后信号断开,座舱旅程怎样补回事件
java·开发语言·前端·数据库·电脑
Wzx1980121 小时前
Python Agent 短期记忆设计:从对话历史到 LangGraph Checkpointer
开发语言·python
定栓1 小时前
Webpack迁到Vite第六节:构建配置详解(vite.config.js)
前端·javascript·webpack
gopher95111 小时前
RUST:错误处理 thiserror 详解
开发语言·后端·rust
破茧蝴蝶9 小时前
Python分布式爬虫完整教程
开发语言·python