| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/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++) {
// 0 4 5
console.log("");
}
JavaScript
for false JavaScript for Java C for
for
for (; ; )
for
for <select>
<form name="selectForm">
<label for="musicTypes"
></label
>
<select id="musicTypes" name="musicTypes" multiple>
<option selected>R&B</option>
<option>Jazz</option>
<option>Blues</option>
<option>New Age</option>
<option>Classical</option>
<option>Opera</option>
</select>
<button id="btn" type="button">How many are selected?</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(`You have selected ${countSelected(musicTypes)} option(s).`);
});
do...while
do
while ();
({ })
true false do...while
do i 5
let i = 0;
do {
i += 1;
console.log(i);
} while (i < 5);
while true while
while ()
false
true false while
({ })
while n 3
let n = 0;
let x = 0;
while (n < 3) {
n++;
x += n;
}
n x x n
n = 1, x = 1n = 2, x = 3n = 3, x = 63 n < 3 true
false while false :
// !
while (true) {
console.log("Hello, world!");
}
:
JavaScript break continue
break switch
break whiledo-whileforswitch break break
break;
break ;
switch 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 whiledo-whileforlabel
continue whiledo-whilefor break continue while for continue continue
continue;
continue ;
i 3 continue while n 13712
let i = 0;
let n = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
n += i;
console.log(n);
}
// :
// 1 3 7 12
continue; 1,3,6,10,15
checkIandJ checkJ continue checkJ continue checkJ false false checkIandJ false checkIandJ 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, "is odd.");
}
console.log("i =", i);
console.log("j =", j);
}
for...in JavaScript for...in
for ( in )
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 Array for...in
for ( of )
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);
}
// Logs: 3 5 7
for...of for...in Object.entries()
const obj = { foo: 1, bar: 2 };
for (const [key, val] of Object.entries(obj)) {
console.log(key, val);
}
// "foo" 1
// "bar" 2
| Web Proxy Viewer | New URL | Original Page |