DEV Community

Cover image for Difference between for...of and for...in loop in JavaScript.

Difference between for...of and for...in loop in JavaScript.

Swastik Yadav on November 11, 2021

Hello Everyone, The difference between for-of and for-in loop really troubled me when I was learning JavaScript. And with this blog I will try to ...
Collapse
 
leob profile image
leob • Edited

I can never remember which one is which, always need to look it up ... do you happen to know a clever mnemonic to remember it?

You could say "of" is for "o"bject and "in" is an "index" (key) but unfortunately that's not good (for ... of does NOT work with objects).

In fact "for of" (not "for in") is for "iterables" ... so this is a mnemonic that's just the wrong way around, lol.

Collapse
 
msid01 profile image
Siddhesh Mungekar

You can remember it as
of - objects iterable
in - enumerable objects

I hope it won't be confusing 😃

Collapse
 
leob profile image
leob

Hmm yeah that might work ... I can't come up with anything better either lol

Thread Thread
 
drbatroni profile image
DrBatroni

I use it like this:
in - iterate in object
of - iterate over something (string/array)

Thread Thread
 
leob profile image
leob

Yes I like that! best one I've seen until now ...

Collapse
 
narayand16 profile image
Narayan Deshmukh

Thanks , this will really help !

Thread Thread
 
swastikyadav profile image
Swastik Yadav

I am glad that this helped, Narayan 😊

Collapse
 
gsharan2901 profile image
Sharan Gudimalla

Exactly, so I remember the opposite way XD. I used to do it in school too😂

Collapse
 
robinpokorny profile image
Robin Pokorny • Edited

To add one more reason why for..in is bad for arrays is that it skips gaps in sparse arrays:

const arr = ['a']
arr[5] = 'b'

for(const value of arr) {
  console.log(value);
}
// -> a
// -> undefined
// -> undefined
// -> undefined
// -> undefined
// -> b

for(const key in arr) {
  console.log(arr[key]);
}
// -> a
// -> b
Enter fullscreen mode Exit fullscreen mode
Collapse
 
epic71 profile image
epic71 • Edited

Very useful

Collapse
 
swastikyadav profile image
Swastik Yadav

Thanks @epic71

Collapse
 
bradtaniguchi profile image
Brad

I used to get these mixed up all the time. Then I came up with a funny/simple saying.

"Use for...in if you want to be in trouble"

The most common use-case is to iterate over an array of things within an Array, where you'd want for...of, so using for...in would give you the "keys", or numbers which can create confusion.

You don't want to be "in" trouble, so don't use for...in. Hope that helps with remembering :D

Collapse
 
lazylad profile image
Tathagat

I have recently used for of to iterate over array and element of array were to passed to a async function as parameter and I need to hold the execution of loop util async function gets resolved I tried many things but at the end for of loop work in this case.

Collapse
 
nanythery profile image
Nadine M. Thêry

I was about to ask what's the difference between forEach, which I tend to use a lot and these other methods. Honestly, I use forEach because I learnt it first and I didn't feel need to learn others haha.
Are these methods a better practice or a refactor method?

Collapse
 
swastikyadav profile image
Swastik Yadav

Hey Nadine,

forEach is an Array method just like Array.prototype.map and Array.prototype.filter. The difference between forEach and other Array methods is that unlike "map" and "filter", "forEach" does not return the array it always returns undefined.

I hope this cleared the confusion.

Thanks for bringing this up in the comments. 😊

Collapse
 
ddasb profile image
Damien Da Silva Bregieiro

For of came with ES2015 and is currently a better practice :

  • More readable
  • No callbacks
  • Faster (Refering to some benchmark but not sure)
  • You can also use .entries() and destructuring
Collapse
 
imiahazel profile image
Imia Hazel

Thanks for the brilliant tutorial. It helped me a lot.

Collapse
 
swastikyadav profile image
Swastik Yadav

Thanks for your kind words Imia. It means a lot to me. 😊

Collapse
 
imiahazel profile image
Imia Hazel

You did really awesome :)

Collapse
 
nanythery profile image
Nadine M. Thêry

Very useful article, thank you!

Collapse
 
swastikyadav profile image
Swastik Yadav

I am glad that you found this useful.

Collapse
 
bhaskerath profile image
Bhaskar A

Nice

Collapse
 
princezakabdull profile image
Engnr. Zaks

I seems to always get it though 😂

Collapse
 
bogdanbatsenko profile image
bogdanbatsenko

Thanx. Nice and clean explanation

Collapse
 
swastikyadav profile image
Swastik Yadav

Glad, that this helped. 😃