๐Ÿ”for-of

`for ( const value of iterable)` syntax. ๐Ÿšง -> TypeError

JSโŸฉ syntax โŸฉ for loops โŸฉ for-of

โญ๏ธ ES6 (2015)

for (const value of iterable) {}      // loop over "values"
for (lvalue of iterable) {}           // lvalue can be used
for (const [key, value] of aMap) {}   // iterate over a Map.

(by default) object is not iterableโ—

let obj = { x: 1, y: 2, z: 3 };
for (let value of obj) { }                 // TypeError

// workarounds
for (let key of Object.keys(obj) { ... }
for (let key in obj) { ... }                // for-in
for (let value of Object.values(obj) { ... }
for (let [key, value] of Object.entries(obj) { ... }

๐Ÿ‘‰ for-in vs. for-of vs. in

Last updated