For In Loop in JavaScript: for...in vs for...of

Easy100% Free~12 mins#for-in#for-of#iteration#object-keys#prototype
Interview Answer (30-Second Summary)

The for in loop in JavaScript walks the enumerable string keys of an object, including keys it inherits through the prototype chain. Own integer-like keys come first in ascending order, then other string keys in insertion order, then inherited ones. For arrays it gives the indexes as strings, so use for...of to loop over values.

Key Learning Objectives
✓

Predict which keys a for...in loop visits, including inherited enumerable ones.

✓

Know the key order: integer-like keys ascending, then string keys in insertion order, then inherited keys.

✓

Explain why for...in gives array indexes as strings and when to use for...of instead.

✓

Loop over only own properties with Object.keys, Object.entries or Object.hasOwn.

The Interview Problem

What do the three loops print, and in what order are the object's keys visited?

1const base = { inherited: true };
2const obj = Object.create(base);
3obj.b = 2;
4obj[10] = 'ten';
5obj.a = 1;
6obj[2] = 'two';
7
8for (const key in obj) {
9 console.log(key);
10}
11
12const arr = ['x', 'y'];
13for (const i in arr) {
14 console.log(typeof i, i);
15}
16for (const v of arr) {
17 console.log(v);
18}
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

2 / 10 / b / a / inherited / string 0 / string 1 / x / y

b / 10 / a / 2 / inherited / string 0 / string 1 / x / y

2 / 10 / b / a / string 0 / string 1 / x / y

2 / 10 / b / a / inherited / number 0 / number 1 / x / y

V8 Engine Execution Trace
Step 1 of 6 (Line 6)

obj is created with base as its prototype, then gets b, 10, a and 2, in that order.

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
obj:{ 2: 'two', 10: 'ten', b: 2, a: 1 }
obj.[[Prototype]]:base { inherited: true }
Console Stream
> [empty]

For In Loop in JavaScript Explained

What the for...in Loop Visits

The for in loop in JavaScript runs once per enumerable string-keyed property of an object, and it also visits enumerable properties inherited from the prototype chain.

  1. Key order: own integer-like keys first, in ascending numeric order (2, 10), then other own string keys in insertion order (b, a), then inherited keys (inherited). Symbol keys and non-enumerable properties are never visited.
  2. Arrays: for...in gives the index keys, and keys are strings, so typeof i is 'string'. It would also pick up any extra enumerable property added to the array. That is why for...of, which reads values through the array's iterator, is the right loop for arrays.
  3. Own keys only: use Object.keys(obj) or Object.entries(obj) with for...of, or skip inherited keys inside for...in with if (!Object.hasOwn(obj, key)) continue;.
Common Traps & Mistakes

Using for...in on an array and doing arithmetic with the index: '1' + 1 is '11', not 2.

Forgetting that inherited enumerable properties are visited too.

Expecting numeric keys to keep insertion order; they are always visited in ascending order first.

FAANG Follow-Up Probes
Probe #1

How would you list only the object's own keys, and what about its Symbol keys?

Probe #2

What does Object.defineProperty(obj, 'hidden', { value: 1, enumerable: false }) change for the loop?

Probe #3

Why can't you use for...of on a plain object, and how would you make an object iterable?

For In Loop in JavaScript Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for for...in vs for...of Loops.

Q1.
What is the for in loop in JavaScript?

for (const key in obj) runs once for every enumerable string-keyed property of obj, including enumerable properties inherited from its prototype chain. Symbol keys and non-enumerable properties are skipped.

for...in iterates property keys of any object. for...of iterates the values produced by an iterable, such as an array, string, Map or Set. On ['x', 'y'], for...in gives '0' and '1'; for...of gives 'x' and 'y'.

Yes. It also visits enumerable properties on the prototype chain, which is why 'inherited' is printed. Object.keys(obj) and Object.entries(obj) return only the object's own enumerable keys.

Modern engines follow the order the spec defines for own keys: integer-like keys in ascending numeric order, then string keys in the order they were added. Inherited keys come after the object's own keys. That is why 2 and 10 print before b and a.

It gives indexes as strings, not numbers, and it also visits any extra enumerable properties added to the array or Array.prototype. Use for...of, forEach or a classic for loop when you want the elements.

Use for (const [key, value] of Object.entries(obj)), or Object.keys(obj).forEach(...). Inside a for...in loop you can also skip inherited keys with if (!Object.hasOwn(obj, key)) continue.

Yes. for...in is a normal loop statement, so break, continue and return (inside a function) work, unlike in forEach.

More JavaScript interview questions for freshers