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
相关推荐
初遇你时动了情1 分钟前
前端使用TensorFlow.js reactjs调用本地模型 实现图像、文本、音频/声音、视频相关识别
前端·javascript·tensorflow
十一.3669 分钟前
66-69 原型对象,toString(),垃圾回收
开发语言·javascript·原型模式
小小鱼儿飞2 小时前
QT音乐播放器18----新歌速递播放、隐藏顶部和底部工具栏、自定义ToolTips
开发语言·qt
穆雄雄2 小时前
Rust 程序适配 OpenHarmony 实践:以 sd 工具为例
开发语言·rust·harmonyos
0***142 小时前
Swift资源
开发语言·ios·swift
z***I3942 小时前
Swift Tips
开发语言·ios·swift
J***Q2922 小时前
Swift Solutions
开发语言·ios·swift
铅笔小新z2 小时前
C++入门指南:开启你的编程之旅
开发语言·c++
Gavin-Wang2 小时前
Swift + CADisplayLink 弱引用代理(Proxy 模式) 里的陷阱
开发语言·ios·swift
AiXed3 小时前
PC微信WDA算法
前端·javascript·macos