주어진 함수를 배열 요소 각각에 대해 실행
array.forEach(callback(currentValue, index, array), thisArg)
- callback : 각 요소에 대해 실행할 함수
- currentValue : 처리할 현재 요소
- index : 처리할 현재 요소의 인덱스
- array : forEach()를 호출한 배열
- thisArg : callback을 실행할 때 this로 사용할 값
const items = ['item1', 'item2', 'item3'];
const copy = [];
// for문
for (let i=0; i<items.length; i++) {
copy.push(items[i]);
}
// forEach문
items.forEach(function(item){
copy.push(item);
})