[javascript] 반복문 : forEach / for in / for of

이아현·2023년 5월 4일
0

javascript

목록 보기
4/14
post-thumbnail

✅ forEach : 배열 요소 각각에 대해 실행

  • 매개변수
    • CurrentValue : 처리할 현재 요소
    • Index : 처리할 현재 요소의 인덱스
    • Array : forEach 메서드를 호출한 배열
  • 사용법
    • array.forEach((CurrentValue, Index, Array) => {callback})
const array = ['a', 'b', 'c'];
array.forEach(element => console.log(element);
// output
// a
// b
// c

✅ for in : 객체에서 문자열로 키가 지정된 모든 열거 가능한 속성 반복

const object = {name: 'ahyun', job: 'engineer'};
for (const property in object) {
	console.log(`${property} : ${object[property]}`)
}
// output
// name : ahyun
// job : engineer

✅ for of : iterable 배열 순회

const arr = [1, 2, 3];
for (const num of arr) {
	console.log(num);
}
// output
// 1, 2, 3
profile
PM을 지향하는 FE 개발자 이아현입니다 :)

0개의 댓글