Wednesday, 16 December 2020

JS - for of only iterable on Array not Object

// Array, for-of will not be executed if array is empty

var b = [];

for (let item of b ) {console.log('here')}

//Result: not executed


// Array, for-of will be executed if array is not empty

var b = [1];

for (let item of b ) {console.log('here ' + item)}

// Result:  here 1


// Object

var c = {'test':1}

for (let item of c ) {console.log('here ' + item)}

//Result Uncaught TypeError: c is not iterable at <anonymous>:1:18

No comments:

Post a Comment