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
相关推荐
用户24171401418604 分钟前
一个 Tapas 菜单 App,逼我啃下了前端存储和 this 绑定两座大山
javascript
小陈的进阶之路7 分钟前
爬虫三大解析库:lxml, jsonpath, bs4
开发语言·笔记·python
wuyk55510 分钟前
第1章:无刷电机核心原理与运行机制
c语言·开发语言·stm32·单片机·嵌入式硬件·机器学习
SomeB1oody13 分钟前
【RustyML入门】2.10. 主成分分析
开发语言·后端·机器学习·rust·教程
xiaominlaopodaren20 分钟前
three.js地图数学基础(二):Web Mercator
javascript·gis·three.js
mONESY22 分钟前
深入理解 React useRef:从 DOM 操作到持久化存值的完整指南
javascript
2603_9651481123 分钟前
抖音/快手直播选品:用API快速匹配爆款与低价货源
开发语言·python·自动化·api
谷哥的小弟24 分钟前
TypeScript接口
javascript·typescript
RobinDevNotes25 分钟前
轻量级动画引擎Anime.js迎来V4大版本更新
开发语言·前端·javascript·ecmascript·动画·c4前端
lv__pf27 分钟前
servlet与jsp
java·开发语言·servlet