카테고리 없음

7. Javascript 흐름 제어(Control Flow Statements)

okthatsimple 2024. 7. 27. 02:09

흐름 제어

  • 조건문과 반복문으로 흐름을 제어함
  • 아래 구문을 다룸
    • return, break, continue, throw, if...else, switch, try...catch
    • for, do...while, while, for...in, for...of
    • C언어, 파이썬과 유사함.

for...in, for...of

  • for in 구문은 파이썬의 for element in target.__dict__["keys"] 구문과 비슷하다.
    • index(in)를 순회한다고 생각하면 된다. 객체의 모든 key를 반환한다.
  • for of 구문은 파이썬의 for element in elements 구문과 비슷하다. 반복 가능한 객체에서 하나씩 값을 순환한다.
let array = [1, 2, 3, 4, 5];
array.foo = 'bar';

// 출력 : 1, 2, 3, 4, 5
for (const item of array) {
    console.log(item);
}

// 출력 : 0, 1, 2, 3, 4, foo
for (const item in array) {
    console.log(item);
}