I know what is for... in
loop (it iterates over key), but heard the first time about for... of
(it iterates over value).
I am confused with
Another difference between the two loops, which nobody has mentioned before:
Destructuring
for...in
is deprecated. Usefor...of
instead.
Source
So if we want to use destructuring in a loop, for get both index and value of each array element, we should to use the for...of
loop with the Array method entries():
for (const [idx, el] of arr.entries()) {
console.log( idx + ': ' + el );
}