[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/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++) {
  //  0  4  5 
  console.log("");
}

JavaScript

for

for false JavaScript for Java C for

for

js
for (; ; )
  

for

  1. 1
  2. for
  3. ({ })
  4. 2

for <select>

HTML

html
<form name="selectForm">
  <label for="musicTypes"
    ></label
  >
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&amp;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>

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(`You have selected ${countSelected(musicTypes)} option(s).`);
});

do...while

do...while

do...while

js
do
  
while ();

({ })

true false do...while

do i 5

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

while

while true while

js
while ()
  

false

true false while

({ })

1

while n 3

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

n x x n

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

3 n < 3 true

2

false while false :

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

break continue

js
:
  

JavaScript break continue

break

break switch

  • break whiledo-whileforswitch
  • break

break

js
break;
break ;
  1. switch
  2. 2

1

theValue

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

2:

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-whileforlabel

  • continue whiledo-whilefor break continue while for
  • continue

continue

js
continue;
continue ;

1

i 3 continue while n 13712

js
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

2

checkIandJ checkJ continue checkJ continue checkJ false false checkIandJ false checkIandJ 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, "is odd.");
  }
  console.log("i =", i);
  console.log("j =", j);
}

for...in

for...in JavaScript for...in

js
for ( in )
  

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

Array

for Array for...in

for...of

for...of ArrayMapSetarguments

js
for ( of )
  

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);
}
// Logs: 3 5 7

for...of for...in Object.entries()

js
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