Videos
for in loops over enumerable property names of an object.
for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.
In your example, the array iterator does yield all the values in the array (ignoring non-index properties).
I found a complete answer at Iterators and Generators (Although it is for TypeScript, this is the same for JavaScript too)
Both
for..ofandfor..instatements iterate over lists; the values iterated on are different though,for..inreturns a list of keys on the object being iterated, whereasfor..ofreturns a list of values of the numeric properties of the object being iterated.Here is an example that demonstrates this distinction:
let list = [4, 5, 6]; for (let i in list) { console.log(i); // "0", "1", "2", } for (let i of list) { console.log(i); // 4, 5, 6 }Another distinction is that
for..inoperates on any object; it serves as a way to inspect properties on this object.for..ofon the other hand, is mainly interested in values of iterable objects. Built-in objects likeMapandSetimplementSymbol.iteratorproperty allowing access to stored values.let pets = new Set(["Cat", "Dog", "Hamster"]); pets["species"] = "mammals"; for (let pet in pets) { console.log(pet); // "species" } for (let pet of pets) { console.log(pet); // "Cat", "Dog", "Hamster" }