DEV Community

Discussion on: Difference between for...of and for...in loop in JavaScript.

Collapse
leob profile image
leob • Edited on

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
gsharan2901 profile image
Sharan Gudimalla

Exactly, so I remember the opposite way XD. I used to do it in school tooπŸ˜‚

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 Author

I am glad that this helped, Narayan 😊