| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration | [Back] [Original] |
Get to know MDN better
JavaScript JavaScript
X Y 5
for (let step = 0; step < 5; step++) {
// 5 step 0 4
console.log("");
}
0
JavaScript
for for falseJavaScript for JavaC for for
for (initialization; condition; afterthought)
statement
for
initializationcondition condition true condition falsefor condition condition truestatement { ... }afterthought for <select> for i 0 i <select> if i 1
<form name="selectForm">
<label for="musicTypes"></label>
<select id="musicTypes" name="musicTypes" multiple="multiple">
<option selected="selected">R&B</option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
<button id="btn" type="button"></button>
</form>
for i 0 i <select> if i 1
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
do
statement
while (condition);
statement { ... } condition truestatement condition false do...while
do i 5
let i = 0;
do {
i += 1;
console.log(i);
} while (i < 5);
while while truewhile
while (condition)
statement
statement while
statement statement while
({ ... })
n 3 while
let n = 0;
let x = 0;
while (n < 3) {
n++;
x += n;
}
n 1 x x n
n = 1x = 1n = 2x = 3n = 3x = 6 n < 3
while
//
while (true) {
console.log("");
}
label label label break continue
label
label:
statement
label JavaScript statement
markLoop while
markLoop: while (theMark == true) {
doSomething();
}
Label
Label
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
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
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
break whiledo-whilefor switch break labelbreak
break;
break label;
[] label switch label
theValue
for (let i = 0; i < a.length; i++) {
if (a[i] === theValue) {
break;
}
}
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
continue whiledo-while for continue label continue
continue;
continue label;
while i 3 continue n 137 12
let i = 0;
let n = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
n += i;
console.log(n);
}
//
// 1 3 7 12
checkIandJ checkJ continue checkJ continue checkJ checkJ false false checkIandJ checkIandJ false false checkIandJ
continue checkIandJ checkIandJ
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
for (variable in object) {
statements
}
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 (variable of object) {
statement
}
for...of for...in for...in for...of
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 |