HardClosures & Scope💻 Output Question

for...in includes inherited properties, for...of does not

💡

Hint

Never use for...in on arrays — it includes non-index properties. Use for...of for values.

What does this output?

const arr = [10, 20, 30];
arr.custom = 'extra';
const forIn = [], forOf = [];
for (const k in arr)  forIn.push(k);
for (const v of arr)  forOf.push(v);
console.log(forIn.join(','));
console.log(forOf.join(','));

Correct Output

0,1,2,custom
10,20,30

Why this output?

Explanation: for...in iterates all enumerable keys including custom properties. for...of uses the iterator and only yields the array elements.

Key Insight: Never use for...in on arrays — it includes non-index properties. Use for...of for values.

More Closures & Scope Output Questions

EasyClassic var in loop with setTimeoutEasylet in loop — each iteration gets own bindingMediumIIFE captures loop variableMediumClosure mutation persists across calls

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz