for..in
- iterates over keys of all objects - loops over enumerable property names of an object.for..of
- iterates over values of iterable objects (Array, TypedArray, String, Map, Set) - does use an object-specific iterator and loops over the values generated by that.forEach
- specific method for arraysmap
- specific method for arrays
Example with an object
1 | const obj = { 1: 1, 2: true, 3: "str" } |
Example with a set
1 | let pets = new Set(["Cat", "Dog", "Hamster"]) |
Example with for..in
and enumerable method
Because for..in
loops over all enumerable properties, this means if you add any additional properties to the array’s prototype, then those properties will also appear in the loop.
1 | Array.prototype.decimalfy = function() { |
Above for for..of
1 | const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |