| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Loops_and_iteration | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
. JavaScript Guide JavaScript .
. X Y . , " 5 " .
var step;
for (step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log("Walking east one step");
}
. . . ( 0 .) .
JavaScript :
for for . JavaScript C . for .
for ([]; []; [])
for , .:
( <select>) for . for i 0 . i <select> i . if i 1 .
<form name="selectForm">
<p>
<label for="musicTypes"
>Choose some music types, then click the button below:</label
>
<select id="musicTypes" name="musicTypes" multiple="multiple">
<option selected="selected">R&B</option>
<option>Jazz</option>
<option>Blues</option>
<option>New Age</option>
<option>Classical</option>
<option>Opera</option>
</select>
</p>
<p><input id="btn" type="button" value="How many are selected?" /></p>
</form>
<script>
function howMany(selectObject) {
var numberSelected = 0;
for (var i = 0; i < selectObject.options.length; i++) {
if (selectObject.options[i].selected) {
numberSelected++;
}
}
return numberSelected;
}
var btn = document.getElementById("btn");
btn.addEventListener("click", function () {
alert(
"Number of options selected: " + howMany(document.selectForm.musicTypes),
);
});
</script>
do...while do...while . do...while .
do
while ();
. { } . , . . , do...while .
, do . i 5 .
do {
i += 1;
console.log(i);
} while (i < 5);
while while . while .
while ()
, .
. , . , while .
, { } .
while n 3 , .
n = 0;
x = 0;
while (n < 3) {
n++;
x += n;
}
, n x . , x n .
n = 1 and x = 1n = 2 and x = 3n = 3 and x = 6, n < 3 , .
. . while .
// .
while (true) {
console.log("Hello, world");
}
:
label :
statement
JavaScript . .
, markLoop while .
markLoop: while (theMark == true) {
doSomething();
}
break break , switch, .
while, do-while, for, switch .break .
break;break [];break switch . .
for (i = 0; i < a.length; i++) {
if (a[i] == theValue) {
break;
}
}
var x = 0;
var z = 0;
labelCancelLoops: while (true) {
console.log("Outer loops: " + x);
x += 1;
z = 1;
while (true) {
console.log("Inner loops: " + z);
z += 1;
if (z === 10 && x === 10) {
break labelCancelLoops;
} else if (z === 10) {
break;
}
}
}
continue while, do-while, for, .
continue :
continue;continuelabel;i 3 continue while . , n 1, 3, 7, 12 .
i = 0;
n = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
n += i;
}
checkiandj checkj . continue , checkj , . false continue , checkj . false , checkiandj , false checkiandj . false , checkiandj .
continue checkiandj , checkiandj .
checkiandj: while (i < 4) {
console.log(i);
i += 1;
checkj: while (j > 4) {
console.log(j);
j -= 1;
if (j % 2 == 0) {
continue checkj;
}
console.log(j + " is odd.");
}
console.log("i = " + i);
console.log("j = " + j);
}
for...in for...in . , JavaScript . for...in :
for (variable in object) {
statements
}
. .
function dump_props(obj, obj_name) {
var result = "";
for (var i in obj) {
result += obj_name + "." + i + " = " + obj[i] + "<br>";
}
result += "<hr>";
return result;
}
make model car , :
car.make = Ford;
car.model = Mustang;
, for...in . Array , for...in , for .
for...of for (variable of object) {
statement
}
for...of for...in . for...in , for...of :
let arr = [3, 5, 7];
arr.foo = "hello";
for (let i in arr) {
console.log(i); // logs "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // logs "3", "5", "7"
}
This page was last modified on 2026 7 15 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |