The distinction between the for..in
and for..of
methods of arrays in JavaScript will be discussed in this blog.
These methods are a simplified form of the actual for
loop, however they should not be used in place of the standard for
loop.
for
loop
for( let i=0; i<length; i++)
This is the standard for
loop which iterates over the length and returns i .
for...in
loop
Synatx: for( let key in obj)
๐ It is for iterating over the "enumerable" properties of an object.
๐ It gives us the keys/properties of an object.
๐ It is mostly used with objects.
๐ It does not have a specific order of execution.
for...of
loop
Synatx: for(let value of array)
๐ It is used to iterate over "iterable".
๐ It is mostly used with strings and arrays
๐ It does not work with objects as they are not "iterable".
โค๏ธ// ๐
This topic had always been a source of uncertainty for me, so I decided to write a blog on it in order to learn more about it so that I might help someone else who was in the same boat.
Do Like and SHARE your valuable feedback.
Connect ๐