Skip to the solution
EasyFrontend5 min read

What is the difference between for...of and for...in?

986 views

The problem

Question presented to candidate: "If I add a custom property directly onto an array, like myArray.total = 100, and then loop over the array with for...in, what actually shows up in the loop? Would for...of behave the same way?"

What a strong answer should cover:

  • πŸ“Œ Interview term: for...of β€” iterates over the values of anything implementing the iterable protocol (arrays, strings, Maps, Sets, generators) β€” it works only on genuinely iterable things, and genuinely does NOT see non-index properties.
  • πŸ“Œ Interview term: for...in β€” iterates over an object's enumerable property keys (as strings), including inherited enumerable properties from the prototype chain β€” it works on any object, not just iterables, and was never really designed for arrays specifically.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: adding myArray.total = 100 and then using for...in genuinely includes "total" as one of the loop's keys, alongside the numeric index keys β€” a real, concrete bug source. for...of, verified directly on the identical array, genuinely does NOT include it at all β€” it only ever produces the array's real element values.
  • πŸ“Œ Interview term: for...of on a non-iterable throws β€” verified directly: using for...of on a plain object (which does not implement the iterable protocol) genuinely throws a real TypeError, while for...in works on it fine.
  • A precise answer names the standard, real guidance this leads to: use for...of for arrays/iterables (to get real values, safely, without picking up stray properties), and reach for for...in only when inherited/enumerable KEYS on a plain object are specifically needed β€” with Object.keys()/Object.entries() as the more precise, own-properties-only alternative in most real cases.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering the prompt's own custom-property scenario with real proof is the strong signal.

Code / implementation expected: Yes β€” reproducing the prompt's exact scenario (a custom array property leaking into for...in) is the clearest, most convincing demonstration.

  • for-of
  • for-in

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: for...in leaks a custom array property into its keys while for...of never sees it

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 8 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What does Array.filter do?

Continue