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

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

. JavaScript Guide JavaScript .

. X Y . , " 5 " .

js
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 :

In this article

for

for . JavaScript C . for .

    for ([]; []; [])
      

for , .:

  1. . 1 . . .
  2. . , . , for . , .
  3. . , { } .
  4. 2 .

( <select>) for . for i 0 . i <select> i . if i 1 .

html
<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 .

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

while

while . while .

    while ()
      

, .

. , . , while .

, { } .

1

while n 3 , .

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

, n x . , x n .

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

, n < 3 , .

2

. . while .

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

. , , break continue .

:

    label :
       statement

JavaScript . .

, markLoop while .

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

break

break , switch, .

  • break : while, do-while, for, switch .
  • : .

break .

  1. break;
  2. break [];

break switch . .

1

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

2: Breaking to a label

js
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

continue while, do-while, for, .

  • continue , while, do-while, for , . break , continue . while . for .
  • continue , continue .

continue :

  1. continue;
  2. continuelabel;

1

i 3 continue while . , n 1, 3, 7, 12 .

js
i = 0;
n = 0;
while (i < 5) {
  i++;
  if (i == 3) {
    continue;
  }
  n += i;
}

2

checkiandj checkj . continue , checkj , . false continue , checkj . false , checkiandj , false checkiandj . false , checkiandj .

continue checkiandj , checkiandj .

js
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
}

. .

js
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 , :

js
car.make = Ford;
car.model = Mustang;

, for...in . Array , for...in , for .

for...of

for...of , (, Map, Set, ) .

    for (variable of object) {
      statement
    }

for...of for...in . for...in , for...of :

js
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"
}

Web Proxy Viewer  |  New URL  |  Original Page