자바스크립트에서 루프(Loop)를 사용하면 코드 블록을 여러번 실핼할 수 있게 해줍니다.
for (statement 1; statement 2; statement 3) {}
// stat 1 = 루프가 시작되기 전에 실행
// stat 2 = 루프 실행을 위한 조건
// stat 3 = 루프 실행된 후마다 실행
for (let i = 0; i < 10; i++) {
if (i === 3) {
console.log(3);
continue;
}
if (i === 5) {
console.log("5 stop the loop");
break;
}
console.log("Number" + i);
}
// 객체의 속성(property)을 따라 반복합니다.
const user = {
name: 'Kim',
province: '경기도',
city: '고양시'
}
for(let x in user) {
console.log(`${x} : ${user[x]}`)
}
// 지정된 조건이 true인 동안 코드 블록을 반복
let i = 0
while(i < 10){
console.log('Number' + i);
i++
}
// do/while 루프는 while 루프의 변형이다.
// 이 루프는 조건이 true인지 검사하기 전에 코드 블록을 한 번 실행
// 그러고 나서 조건이 true인 동안 루프를 반복합니다.
let i = 0;
do {
console.log('Number' + i)
i++
}
while(i < 10)
let i = 100;
do {
console.log('Number' + i)
i++
}
while(i < 10)
// Loop Throuh array
const locations = ['서울', '부산', '경기도', '대구'];
// for
for(let i = 0; i < locations.lenth; i++) {
console.log(locations[i])
}
// foreach
locations.forEach(function (location, index, array) {
console.log(`${index} : ${location}`)
console.log(array)
})
// map
locations.map(function (location){
console.log(location)
})