JS: 반복문 메서드 (while, do while, for, forEach )

sungji·2024년 2월 7일
0

while

조건이 참일 때 계속 반복한다. 조건이 거짓이면 넘어간다.
statement를 여러개 사용하려면 { } 를 써야 한다.

while(condition) {
statement
}

do while

실행 먼저 되고 조건이 참인지 확인한다. (무조건 한 번은 실행)

do{
statement
} while(condition)

for

for는 반복 횟수를 정하는 경우에 사용한다.

for(let i; i<5; i++) {
statement
}
  • initialization : 카운트할 변수 선언, let은 지역변수, var는 for문과 같은 범위
  • condition: 매번 반복할 때마다의 조건, 참이면 statement를 반복, 거짓이면 다음식으로 넘어감
  • final-expression: 매번 반복이 끝난 후의 동작(주로 카운트 변수 증감)

forEach()

요소를 반복할 때 사용하는 메서드
반복가능한 객체는? array, map, set, string, typedarray, argument 등

let iterable = [10, 20, 30]
iterable.forEach((a, i)=>{statement})

variable은 속성값 ( a는 반복할 수 있는 속성값. 1, 2, 3 이라면 num 이런 식으로 사용)

profile
열정 열정 열정

0개의 댓글