Difference b/w for..in & for..of

Difference b/w for..in & for..of

ยท

1 min read

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

carbon.png

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

carbon (1).png

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 ๐Ÿ”—

Twitter

ย