[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JavaScript JavaScript

X Y 5

js
for (let step = 0; step < 5; step++) {
  //  5 step  0  4
  console.log("");
}

0

JavaScript

for

for falseJavaScript for JavaC for for

js
for (initialization; condition; afterthought)
  statement

for

  1. initialization
  2. condition condition true condition falsefor condition condition true
  3. statement { ... }
  4. afterthought
  5. 2

for <select> for i 0 i <select> if i 1

HTML

html
<form name="selectForm">
  <label for="musicTypes"></label>
  <select id="musicTypes" name="musicTypes" multiple="multiple">
    <option selected="selected">R&amp;B</option>
    <option></option>
    <option></option>
    <option></option>
    <option></option>
    <option></option>
  </select>
  <button id="btn" type="button"></button>
</form>

JavaScript

for i 0 i <select> if i 1

js
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById("btn");

btn.addEventListener("click", () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(` ${countSelected(musicTypes)} `);
});

do...while

do...while false

do...while

js
do
  statement
while (condition);

statement { ... } condition truestatement condition false do...while

do i 5

js
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);

while

while truewhile

js
while (condition)
  statement

statement while

statement statement while

({ ... })

1

n 3 while

js
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}

n 1 x x n

  • n = 1x = 1
  • n = 2x = 3
  • n = 3x = 6

n < 3

2

while

js
// 
while (true) {
  console.log("");
}

label

label label break continue

label

js
label:
  statement

label JavaScript statement

markLoop while

js
markLoop: while (theMark == true) {
  doSomething();
}

Label

Label

js
var num = 0;
for (var i = 0; i < 10; i++) {
  // i 
  for (var j = 0; j < 10; j++) {
    // j 
    if (i == 5 && j == 5) {
      break; // i = 5j = 5  j 
    } //  i  j 
    num++;
  }
}

alert(num); //  95

Label

js
var num = 0;
outPoint: for (var i = 0; i < 10; i++) {
  for (var j = 0; j < 10; j++) {
    if (i == 5 && j == 5) {
      break outPoint; //  i = 5j = 5 
      //  outPoint 
    }
    num++;
  }
}

alert(num); //  55

continue label

js
var num = 0;
outPoint: for (var i = 0; i < 10; i++) {
  for (var j = 0; j < 10; j++) {
    if (i == 5 && j == 5) {
      continue outPoint;
    }
    num++;
  }
}
alert(num); // 95

alert(num) continue outPoint; outPoint for

break

break switch label

  • label break whiledo-whilefor switch
  • label break label

break

js
break;
break label;

[] label switch label

1

theValue

js
for (let i = 0; i < a.length; i++) {
  if (a[i] === theValue) {
    break;
  }
}

2 label

js
let x = 0;
let z = 0;
labelCancelLoops: while (true) {
  console.log("" + x);
  x += 1;
  z = 1;
  while (true) {
    console.log("" + z);
    z += 1;
    if (z === 10 && x === 10) {
      break labelCancelLoops;
    } else if (z === 10) {
      break;
    }
  }
}

continue

continue whiledo-whilefor label

  • label continue whiledo-while for
  • label continue label

continue

js
continue;
continue label;

1

while i 3 continue n 137 12

js
let i = 0;
let n = 0;
while (i < 5) {
  i++;
  if (i === 3) {
    continue;
  }
  n += i;
  console.log(n);
}
// 
// 1 3 7 12

2

checkIandJ checkJ continue checkJ continue checkJ checkJ false false checkIandJ checkIandJ false false checkIandJ

continue checkIandJ checkIandJ

js
let i = 0;
let j = 10;
checkIandJ: while (i < 4) {
  console.log(i);
  i += 1;
  checkJ: while (j > 4) {
    console.log(j);
    j -= 1;
    if (j % 2 === 0) {
      continue;
    }
    console.log(j, "");
  }
  console.log("i =", i);
  console.log("j =", j);
}

for...in

for...in JavaScript

js
for (variable in object) {
  statements
}

js
function dumpProps(obj, objName) {
  let result = "";
  for (const i in obj) {
    result += `${objName}.${i} = ${obj[i]}<br>`;
  }
  result += "<hr>";
  return result;
}

make model car result

car.make = Ford
car.model = Mustang

for...in Array for for...in for...of forEach()

for...of

for...of ArrayMapSetarguments

js
for (variable of object) {
  statement
}

for...of for...in for...in for...of

js
const arr = [3, 5, 7];
arr.foo = "hello";

for (const i in arr) {
  console.log(i); //  "0", "1", "2", "foo"
}

for (const i of arr) {
  console.log(i); //  "3", "5", "7"
}

//  for...of  "hello"

Web Proxy Viewer  |  New URL  |  Original Page