var brands = ['Apple', 'Coca-Cola', 'Starbucks', 'Amazon', 'Disney'
위에 적힌 브랜드들을 하나씩 출력해주고 싶으면
console.log(brands[0]);
console.log(brands[1]);
console.log(brands[2]);
console.log(brands[3]);
console.log(brands[4]);
이런식으로 하나씩 출력해 줄 수 있다.
그런데 이렇게 5개가 아니라 100개 1000개 일 경우 하나씩 써주는게 아니라 반복문을 써주면 된다.
강의때 배운 반복문중 for문을 예시로 적어보자..!
for(var i = 0; i < 4; i = i + 1){
console.log(breands[i]);
}
var i = 0; : 반복문에 사용할 변수 선언
i = 4; : 반복 조건
i = i + 1 : 반복문이 끝나고 실행될 코드 (코드가 반복되다가 더 이상 조건 부분을 통과하지 못할 경우 for문은 끝나게 된다!
console.log(breands[i]) : 반복시키고 싶은 코드
결과 값
Apple
Coca-Cola
Starbucks
Amazon
Disney
이렇게 반복문을 사용하면 좀 더 쉽고 빠르게 배열할 수 있다.