MDN
참고블로그
같거나 비슷한 코드를 여러번 실행시킬 경우
동일한 작업 반복
<출저 MDN>
for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement
for (let i = 0; i < 10; i++){
console.log(i); // 0~9까지 출력
}
do
statement
while (condition);
do{
console.log('일단 한번은 실행된다.'); // 이 코드만 한번 실행되고 반복 종료.
}while(false);
while (condition)
statement
let num = 0;
while(num <3){
console.log(num); // 0~2 까지 출력
num++;
}
for (counter = 1; counter < 4; counter++) { // count from 1 to 3 three times
console.log("Count from 1 to 3");
for (counterTwo = 1; counterTwo < 4; counterTwo++){
console.log(counterTwo);
}
}
---------------------------
"Count from 1 to 3"
1
2
3
"Count from 1 to 3"
1
2
3
"Count from 1 to 3"
1
2
3
label :
statement
ex) the label markLoop identifies a while loop.
markLoop:
while (theMark === true) {
doSomething();
}
break; // he innermost enclosing loop or switch.
break [label]; // terminates the specified labeled statement.
ex 1)
for (let i = 0; i < a.length; i++) {
if (a[i] === theValue) {
break;
}
}
ex 2)
let x = 0;
let 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 [label]; //
for (variable in object)
statement
const obj = {
name: 'nara',
hobby: 'drawing'
}
for (const key in obj){
console.log(`${key} : ${obj[key]}`);
} // name : nara
// hobby : drawing
for (variable of object)
statement
const arr = [10, 20, 30];
for (const item of arr){
console.log(item); // 10, 20, 30
}
[10, 20, 30].forEach((value, index, array)=>{
console.log(`${index} : ${value}`); // 0 : 10, 1 : 20, 2: 30
})