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
相关推荐
HackTwoHub27 分钟前
AI赋能Chrome MCP × JS逆向Skill自动化JS逆向挖洞
javascript·人工智能·chrome·安全·web安全·网络安全·自动化
彦楠30 分钟前
指定Tomcat运行的JDK地址
java·开发语言·tomcat
csbysj202032 分钟前
Rust 组织管理
开发语言
清水白石00836 分钟前
构建企业级 Python 服务:配置、日志、指标与追踪的稳健之道
开发语言·python·elasticsearch
lsx20240636 分钟前
特效(Effect)
开发语言
mengqudoh42 分钟前
elementui el-table 表头固定功能
javascript·vue.js·elementui
YiWait44 分钟前
基于 Vue 3 的网络收音机,编译为桌面应用软件
前端·javascript·vue.js
那小子、真烦1 小时前
Hermes Agent Chat 方法分析
java·开发语言
爱喝水的鱼丶1 小时前
SAP-ABAP:变量、常量、结构与内表声明(10篇博客合集) 第六篇:ABAP 7.40+新特性:声明语法的简化写法与兼容注意事项
运维·服务器·开发语言·学习·算法·sap·abap
上海合宙LuatOS1 小时前
Air8000低功耗指南
开发语言·物联网·php·lua