Javascript (ES6)

Javascript ES6 For in과 For of (반복문)

Sorrynthx 2020. 7. 25. 00:13

 

var data1 = ['1', NaN, 'test', 01];

for (let idx in data1) {
    console.log(data1[idx]);
}
// 1, NaN, test, 1

for (let value of data1) {
    console.log(value);
}
// 1, NaN, test, 1

 

for in 반복문은 data1 변수를 iterate(반복하다)하면서 index를 출력 ---> console.log(data1[index]);

=> basic For도 같음 for(var i=0; i<4; i++){ }

 

for of 반복문은 data1 변수를 iterate(반복하다)하면서 해당 값을 출력 ---> console.log(value);

=> forEach도 같음 array.forEach(function(value){ });