for.. in 과 for.. of의 차이

forinforof

for...of 반복문 ( 배열 ):

for...of 반복문은 주로 배열(Array) 또는 이터러블 객체(Iterable)를 순회하는데 사용됩니다

const colors = ["red", "green", "blue"];

for (const color of colors) {
  console.log(color);
}


for...in 반복문 ( 객체 ):

for...in 반복문은 객체의 열거 가능한 속성(프로퍼티)을 순회할 때 주로 사용됩니다.

const person = {
  name: "John",
  age: 30,
  job: "engineer"
};

for (const key in person) {
  console.log(key + ": " + person[key]);
}
Oct 9, 2023 Views 70