Full answer
Interview puzzles are short, but each one hides a single rule. The method for answering is to name the rule before giving the output.
Loop closures: a var loop variable is one variable for the whole function. Callbacks created in the loop all read it later, after the loop has ended, so they see the final value. A let loop variable is created fresh for each iteration, so each callback keeps its own value. The old fix was an immediately invoked function that captured the value as a parameter.
Hoisting clash: a function declaration is hoisted with its value, and a var with the same name is hoisted without overriding it. So at the start, typeof shows function. A later assignment such as var g = 1 then replaces the function at its line, so the type becomes number afterwards.
['1', '2', '3'].map(parseInt): map calls its callback with (value, index, array). parseInt(value, radix) takes a radix as the second argument, so the calls are parseInt('1', 0), parseInt('2', 1), and parseInt('3', 2). Radix 0 means 10, radix 1 is invalid, and '3' is not a valid digit in base 2. The result is [1, NaN, NaN].
'b' + 'a' + +'a' + 'a': the unary plus turns 'a' into NaN, and joining it to text gives baNaNa.
Object keys: keys become strings, so every plain object used as a key turns into [object Object] and the entries overwrite each other.
'use strict';
const withVar = [];
for (var i = 0; i < 3; i++) withVar.push(() => i);
const withLet = [];
for (let j = 0; j < 3; j++) withLet.push(() => j);
const withIife = [];
for (var k = 0; k < 3; k++) withIife.push(((copy) => () => copy)(k));
console.log(withVar.map((fn) => fn()), withLet.map((fn) => fn()), withIife.map((fn) => fn()));
console.log(typeof clash, typeof later);
function clash() {}
var clash;
var later = 1;
function later() {}
console.log(typeof later);
console.log(['1', '2', '3'].map(parseInt));
console.log(['1', '2', '3'].map((value) => parseInt(value, 10)));
console.log(['1', '2', '3'].map(Number));
console.log('b' + 'a' + +'a' + 'a');
console.log(typeof typeof 1, typeof NaN, [] + [] === '');
console.log(0.1 * 3 === 0.3, 9 > 8 > 7, 1 < 2 < 3);
const keyed = {};
keyed[{ id: 1 }] = 'first';
keyed[{ id: 2 }] = 'second';
console.log(Object.keys(keyed), keyed['[object Object]']);
const obj = {
value: 10,
regular() {
return [1].map(function () { return this === undefined; })[0];
},
arrow() {
return [1].map(() => this.value)[0];
},
};
console.log(obj.regular(), obj.arrow());Run with Node.js v24.22.1 as js-50.js; two runs gave identical output. Output: [ 3, 3, 3 ] [ 0, 1, 2 ] [ 0, 1, 2 ] function function number [ 1, NaN, NaN ] [ 1, 2, 3 ] [ 1, 2, 3 ] baNaNa string number true false false true [ '[object Object]' ] second true 10
Key points
- Name the rule first, then give the output.
- var is function-scoped and shared; let is per iteration.
- A function declaration wins at the start; a later var assignment overwrites it.
- map passes value, index, and array, so parseInt receives a radix.
- Unary plus on a non-numeric string gives NaN; plain objects as keys become "[object Object]".
Common mistakes
- Guessing outputs without stating the rule that decides them.
- Forgetting that map passes three arguments to the callback.
- Assuming the function declaration and the var of the same name conflict with an error.
- Thinking let in a for loop is shared like var.
Follow-ups
- How would you fix the var loop without changing it to let?
- What does ['10', '10', '10'].map(parseInt) return?
- Why is 9 > 8 > 7 false, while 1 < 2 < 3 is true?
Tips
- Say the rule first; interviewers grade the reasoning more than the result.
- Run unclear cases in the console during practice and keep a short list of the traps.
- Admit when a case is outside what you know and explain the part you are sure of.