배열 등을 순회하며 특정 동작을 실행시킬 수 있다.
for of
내용을 바꾸지 않는다면 let을 const로 바꿔도 무방하다.
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]
for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3
사용 예시
티스토리 hELLO 스킨 날짜 형식 바꾸기 / for of
[# #_article_rep_simple_date_# #] 위가 글 본문 날짜의 원본이고 아래가 변경본이다. ## 부분이 띄어쓰기 처리된 것은 저렇게 하지않으면 포스팅 내에도 치환자가 반영이 되기 때문이다. , [# #_article_rep_da
energneer.tistory.com
forEach
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
중간에 루프를 끝내기 어렵다는 특징이 있다.
Reference