7

I always confuse `for..in` with `for..of` and have to look up their respective meaning. Again.

It drives me crazy.

Do you have any mnemonic on this? How do you remember the difference?

(Now, as in this very moment, I know that for..in is for looping over keys of an object and for..of for looping over iterators. But tomorrow I will have forgotten it again.)

Comments
  • 6
    Easy:
    - the "i" in "in" stands for "index"
    - the "o" in "of" stands for "object" (may be sth else, but you get the point)
  • 1
    @PonySlaystation for..of is NOT for objects
  • 2
    @k0pernikus obviously. Read the comment in brackets. 😉

    edit:
    Sry I was not clear enough. I was refering to what the loop variable will contain depending on "in" or "of".
  • 4
    @PonySlaystation my way to remember it is similar:

    - of: for every element "of" the array
    - in: for every number "in" the "in"dices

    Edit: though I think of it as an array the mnemonic usually works for everything.
  • 1
    For element of iterable
    For member of enumerable
  • 3
    I got so used to for..of that I know it's the regular for each. So for..in is the other one lol
  • 0
    in object
    of iterable

    :)
  • 0
    @SortOfTested
    Goddamn autocorrect 😠
  • 0
    @SortOfTested Hugs :)

    Seemed non intentional
  • 2
    Yeah I always mix these up too. Sometimes I remember by thinking "in" sounds like that start of "enumerable properties", or you look "in an object".

    But I still have to look them up, because I almost never use them. I prefer to just use Object.keys and then map or whatever.
Add Comment