A Mnemonic That Finally Makes for…in vs for…of Stick

Published: (January 12, 2026 at 01:58 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Intro

There are many articles out there explaining the difference between for...in and for...of. I won’t get into that here.
Instead, this is a simple article that answers a question I keep getting asked…over…and over…and over:

How do I remember when to use for...in vs for...of?

💡 for...in sounds like foreign (as in foreign keys).

  • Use for...in to iterate over the keys of an object.
  • Use for...of to iterate over the elements of a collection.

That’s it. Nothing fancy. If you want more info on how this connects back to the official definitions, keep reading.

According to the docs:

Reasoning

Based on the definition of for...in, it is used to iterate over the properties of an object. Object properties are also called keys. In fact, Object.keys() will return the properties of an object.

So I simply think for...in sounds like “foreign”; as in “foreign keys”. If you’re familiar with databases, the term foreign key shouldn’t be foreign to you (pun intended).

Therefore, for...in is for iterating over keys. This means the other one (for...of) must be for iterating over collection elements.

📌 for...in: think “foreign keys”

Back to Blog

Related posts

Read more »

Bootstrapping Bun

Article URL: https://walters.app/blog/bootstrapping-bun Comments URL: https://news.ycombinator.com/item?id=46681309 Points: 3 Comments: 0...