What is the difference between for...in and for...of?
Answer
for...in iterates over the enumerable property keys of an object (including inherited ones from the prototype chain). It is designed for objects, not arrays — though it works on arrays, it can iterate inherited properties and the order is not guaranteed. Best used for plain objects: for (const key in obj) { if (obj.hasOwnProperty(key)) { console.log(key, obj[key]); } }. for...of (ES6) iterates over the values of any iterable (arrays, strings, Maps, Sets, generators, NodeLists). It respects the iteration protocol (Symbol.iterator). Use it for arrays: for (const item of array) { }. It does NOT work on plain objects (they are not iterable). For arrays, for...of is cleaner than for...in. For both key and value: for (const [key, value] of Object.entries(obj)) or for (const [i, val] of array.entries()).