Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

Tricky JavaScript Output Questions for Interviews (With Explanations)

12 tricky "predict the output" JavaScript interview questions with step-by-step explanations: hoisting, temporal dead zone, type coercion, == vs ===, typeof, object references, this, event loop order, array quirks, floating point and ASI.

Sumit KumarUpdated Oct 12, 20266 min readJavaScript
Tricky JavaScript Output Questions for Interviews (With Explanations)

Output-based questions are popular in JavaScript interviews because they reveal whether you understand the language's rules or just memorised syntax. For each of the 12 puzzles below, try to predict the output before reading the answer. The explanations show the reasoning interviewers expect you to say out loud.

Topics behind output-based JavaScript interview questions: hoisting with var and let, type coercion with plus and loose equality, async ordering of sync code, promises and setTimeout, and scope in loops and arrow functions
How to approach any output question

Go line by line. Ask: is anything hoisted? Is there type coercion? What is this? Is anything asynchronous? Write the synchronous output first, then microtasks, then timers.

Hoisting and scope#

Q1. Hoisting with var and functions#

JavaScript
console.log(a);
var a = 5;

greet();
function greet() { console.log("hello"); }

sayHi();
var sayHi = function () { console.log("hi"); };

Output:

Text
undefined
hello
TypeError: sayHi is not a function
  • var a is hoisted and initialised to undefined; the assignment stays in place.
  • Function declarations are hoisted completely, so greet() works.
  • For a function expression, only the var sayHi declaration is hoisted, so it is undefined when called.

Q2. The temporal dead zone#

JavaScript
let x = 1;
{
  console.log(x);
  let x = 2;
}

Output: ReferenceError: Cannot access 'x' before initialization

The inner let x is hoisted to the top of the block, so it shadows the outer x. But it is in the temporal dead zone until its declaration line runs, so reading it throws. The outer x is never consulted.

Q3. Shadowing inside a function#

JavaScript
var x = 1;
function test() {
  console.log(x);
  var x = 2;
}
test();

Output: undefined

Inside test, var x is hoisted to the top of the function, creating a local x that is undefined at the time of the log. The global x is shadowed.

Types and coercion#

Q4. The + operator and coercion#

JavaScript
console.log(1 + "2" + 3);
console.log(1 + 2 + "3");
console.log("5" - 2);
console.log("5" * "2");
console.log(true + true);
console.log([] + []);
console.log([] + {});
console.log(+"");

Output:

Text
123
33
3
10
2
(empty string)
[object Object]
0
  • + is evaluated left to right. If either side is a string, it concatenates: 1 + "2" is "12", then "123". But 1 + 2 is 3 first, then "33".
  • -, * and / only work on numbers, so strings are converted.
  • true becomes 1.
  • Arrays convert to strings ([] becomes ""), and plain objects become "[object Object]".
  • Unary + converts to a number; an empty string is 0.

Q5. Loose equality surprises#

JavaScript
console.log(null == undefined);
console.log(null === undefined);
console.log(NaN == NaN);
console.log("0" == false);
console.log(0 == "");
console.log([] == false);
console.log([] == ![]);

Output: true false false true true true true

  • null and undefined are loosely equal only to each other.
  • NaN is not equal to anything, including itself. Use Number.isNaN.
  • With ==, booleans become numbers: false is 0; "0" becomes 0.
  • [] == false: [] becomes "", then 0; false is 0.
  • ![] is false (an array is truthy), so it becomes the previous case.
Takeaway

Use === by default. The one common exception is value == null, which checks for both null and undefined.

Q6. typeof quirks#

JavaScript
console.log(typeof null);
console.log(typeof NaN);
console.log(typeof []);
console.log(typeof function () {});
console.log(typeof undeclaredVariable);
console.log(typeof typeof 1);

Output: object number object function undefined string

  • typeof null === "object" is a famous bug from the first version of JavaScript, kept for compatibility.
  • NaN is a numeric value meaning "not a valid number".
  • Use Array.isArray to detect arrays.
  • typeof on an undeclared variable doesn't throw; it returns "undefined".
  • typeof 1 is the string "number", and typeof "number" is "string".

References, this and async#

Q7. Objects are passed by reference value#

JavaScript
const a = { x: 1 };
const b = a;
b.x = 2;
console.log(a.x);

let c = { x: 1 };
let d = c;
d = { x: 3 };
console.log(c.x);

function change(obj) {
  obj.x = 10;
  obj = { x: 20 };
}
const e = { x: 1 };
change(e);
console.log(e.x);

Output: 2, 1, 10

  • a and b point to the same object, so mutating through one is visible through the other.
  • Reassigning d makes it point to a new object; c is untouched.
  • The function receives a copy of the reference. Mutating obj.x changes the original, but reassigning obj only changes the local parameter.

Q8. this in methods, arrows and callbacks#

JavaScript
const obj = {
  name: "A",
  regular() { return this.name; },
  arrow: () => this?.name,
  nested() { return [1].map(() => this.name); },
};

console.log(obj.regular());
console.log(obj.arrow());
console.log(obj.nested());

Output (in an ES module or Node.js): A, undefined, ["A"]

  • obj.regular() uses implicit binding: this is obj.
  • The arrow property takes this from the outer (module) scope, not from obj.
  • The arrow inside nested inherits this from nested, which was called on obj.

More practice: this, call, apply and bind interview questions.

Q9. Sync, promises, async/await and timers#

JavaScript
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
(async () => {
  console.log("D");
  await null;
  console.log("E");
})();
console.log("F");

Output: A D F C E B

  1. Synchronous: A, then the async function runs synchronously until await, logging D, then F.
  2. Microtasks in the order they were queued: C, then the continuation after await, E.
  3. Macrotask: B.

Full explanation in event loop interview questions.

Arrays, numbers and syntax#

Q10. Array quirks#

JavaScript
const arr = [1, 2, 3];
arr[10] = 11;
console.log(arr.length);

console.log([1, 2, 3] + [4, 5]);
console.log([10, 1, 3].sort());
console.log(["1", "2", "3"].map(parseInt));

Output:

Text
11
1,2,34,5
[1, 10, 3]
[1, NaN, NaN]
  • Assigning index 10 creates empty slots; length becomes 11.
  • Arrays become strings ("1,2,3" and "4,5") and are concatenated.
  • Default sort compares strings, so "10" comes before "3".
  • map passes (value, index), so the calls are parseInt("1", 0) (radix 0 means default: 1), parseInt("2", 1) (invalid radix: NaN) and parseInt("3", 2) ("3" isn't a binary digit: NaN). Use .map(Number).

Q11. Floating point and Math edge cases#

JavaScript
console.log(0.1 + 0.2 === 0.3);
console.log(0.1 + 0.2);
console.log(Math.max());
console.log(Math.min());
console.log(9007199254740992 === 9007199254740993);

Output:

Text
false
0.30000000000000004
-Infinity
Infinity
true
  • Numbers are 64-bit binary floating point, so 0.1 and 0.2 can't be stored exactly. Compare with a tolerance: Math.abs(a - b) < Number.EPSILON, or work in integer paise/cents.
  • Math.max() with no arguments returns the identity value for "maximum": -Infinity.
  • Integers above Number.MAX_SAFE_INTEGER (253 − 1) lose precision; use BigInt.

Q12. Automatic semicolon insertion and object keys#

JavaScript
function getConfig() {
  return
  {
    debug: true
  };
}
console.log(getConfig());

const store = {};
store[{ a: 1 }] = "first";
store[{ b: 2 }] = "second";
console.log(store);

Output:

Text
undefined
{ "[object Object]": "second" }
  • JavaScript inserts a semicolon after a bare return at the end of a line, so the function returns undefined. Keep the opening brace on the same line as return.
  • Plain object keys must be strings or Symbols. Both objects convert to the same key, "[object Object]", so the second assignment overwrites the first. Use a Map for object keys.

Quick revision#

RuleRemember
var hoistingDeclared as undefined
let/const hoistingTemporal dead zone until declared
+ with a stringConcatenation
NaN == NaNfalse
typeof null"object"
Async orderSync → microtasks → timers
0.1 + 0.2Not exactly 0.3

Keep practising with closures and scope interview questions and JavaScript interview questions for freshers.

Key takeaways#

  • Function declarations hoist fully; var hoists as undefined; let and const sit in the temporal dead zone.
  • + concatenates if either side is a string; other arithmetic operators convert to numbers.
  • Use ===; remember NaN never equals itself and typeof null is "object".
  • Objects are shared by reference, but reassigning a variable or parameter doesn't affect the original.
  • Default sort is string-based, floats are imprecise, and a line break after return returns undefined.