![Cover image for Difference between for...of and for...in loop in JavaScript.](https://media.dev.to/cdn-cgi/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyifd3xneq7wkpbgi1rr.png)
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 ...
For further actions, you may consider blocking this person and/or reporting abuse
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.
You can remember it as
of - objects iterable
in - enumerable objects
I hope it won't be confusing 😃
Hmm yeah that might work ... I can't come up with anything better either lol
I use it like this:
in - iterate in object
of - iterate over something (string/array)
Yes I like that! best one I've seen until now ...
Thanks , this will really help !
I am glad that this helped, Narayan 😊
Exactly, so I remember the opposite way XD. I used to do it in school too😂
To add one more reason why for..in is bad for arrays is that it skips gaps in sparse arrays:
Very useful
Thanks @epic71
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 usingfor...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 :DI 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.
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?
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. 😊
For of came with ES2015 and is currently a better practice :
Thanks for the brilliant tutorial. It helped me a lot.
Thanks for your kind words Imia. It means a lot to me. 😊
You did really awesome :)
Very useful article, thank you!
I am glad that you found this useful.
Nice
I seems to always get it though 😂
Thanx. Nice and clean explanation
Glad, that this helped. 😃