LearnFrenzy
Sign in Take a free test

JavaScript interview · Set 8 of 10

JavaScript Output-Based and Tricky Questions

  • 6 follow-up questions
  • 4 key points
  • Code examples
Test yourself

One rule per question

Every output question tests one rule — hoisting, the event loop, this, coercion or closures. Name the rule as you answer and you can handle any variation.

  • ScopeHoisting and closures in loops.
  • AsyncEvent loop and await ordering.
  • Valuesthis, object keys and coercion.

Quick-fire output questions are a staple of front-end interviews and online tests. Each one below tests a single rule. Read the code, predict the output, then check the explanation — and remember the rule rather than the answer.

1. What is logged? (hoisting)

undefined, then "function". The var declaration is hoisted with value undefined; the function declaration is hoisted completely.

console.log(a);
console.log(typeof greet);
var a = 5;
function greet() {}

2. What is logged? (event loop order)

A, D, C, B. Synchronous code first (A, D), then all microtasks (the promise callback C), then the next macrotask (the timer B).

console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');

3. What is logged? (async/await ordering)

1, 2, 4, 3. An async function runs synchronously until its first await; the rest continues as a microtask after the current synchronous code finishes.

async function run() {
  console.log(2);
  await null;
  console.log(3);
}
console.log(1);
run();
console.log(4);

4. What is logged? (this in an arrow function inside an object)

undefined (in a module or strict script). Arrow functions do not get their own this; an arrow defined directly in an object literal uses the surrounding scope's this, not the object.

const user = {
  name: 'Asha',
  greet: () => console.log(this?.name),
  hello() { console.log(this.name); },
};
user.greet();   // undefined
user.hello();   // 'Asha'

5. What is logged? (object keys)

456. Object keys are strings; both object keys are converted to the same string "[object Object]", so the second assignment overwrites the first. Use a Map for object keys.

const a = {};
const b = { key: 'b' };
const c = { key: 'c' };
a[b] = 123;
a[c] = 456;
console.log(a[b]);

6. What is logged? (closures with var in a loop)

3, 3, 3. One shared var i; by the time the timers run the loop has ended. With let, each iteration gets its own binding and it prints 0, 1, 2.

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}

7. What is logged? (promise chaining)

1, then 3. A then that returns nothing passes undefined; the first then returns 2 but the second callback ignores its input and returns 3, which the third logs. Watch what each callback returns — it becomes the next value.

Promise.resolve(1)
  .then(v => { console.log(v); return v + 1; })
  .then(() => 3)
  .then(v => console.log(v));

8. What is logged? (typeof and coercion mix)

"number", "53", 2, true. typeof NaN is "number"; + with a string concatenates; - converts to numbers; null == undefined is true.

console.log(typeof NaN);
console.log('5' + 3);
console.log('5' - 3);
console.log(null == undefined);

Remember

Key points

  • Sync → microtasks → macrotasks

    The event loop order.

  • await splits the function

    The rest runs as a microtask.

  • Arrows borrow this

    Not the object they sit in.

  • Object keys are strings

    Use Map for object keys.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What does console.log(0.1 + 0.2 === 0.3) print? Basic

    false — floating-point rounding gives 0.30000000000000004.

  2. 2 What does [1, 2, 3].map(parseInt) return? Advanced

    [1, NaN, NaN] — map passes (value, index), so parseInt receives the index as the radix: parseInt("2", 1) and parseInt("3", 2) are NaN.

  3. 3 What does console.log([] + []) print? Intermediate

    An empty string — both arrays convert to "" and are concatenated.

  4. 4 What is logged: let x = 1; { console.log(x); let x = 2; }? Intermediate

    A ReferenceError — the inner let x is in its temporal dead zone at the log line, even though an outer x exists.

  5. 5 What does typeof typeof 1 return? Basic

    "string" — typeof always returns a string.

  6. 6 What does console.log(1 < 2 < 3, 3 > 2 > 1) print? Intermediate

    true false — comparisons run left to right: 3 > 2 is true, and true > 1 becomes 1 > 1, which is false.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links