Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

Predict the output

JavaScript output questions. Predict, then prove it.

Tricky snippets on the event loop, closures, hoisting, this, promises and type coercion, the way interviewers ask them. Pick an answer, then read why.

155questions
START HEREWhat's the output?
Answer now →
1–12 of 155

What's the output?

4 options
function sayHi() {
  console.log(name);
  console.log(age);
  var name = 'Lydia';
  let age = 21;
}
…
A · `Lydia` and `undefined`B · `Lydia` and `ReferenceError`C · `ReferenceError` and `21`D · `undefined` and `ReferenceError`
Answer →

What's the output?

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

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
…
A · `0 1 2` and `0 1 2`B · `0 1 2` and `3 3 3`C · `3 3 3` and `0 1 2`
Answer →

What's the output?

4 options
const shape = {
  radius: 10,
  diameter() {
    return this.radius * 2;
  },
  perimeter: () => 2 * Math.PI * this.radius,
…
A · `20` and `62.83185307179586`B · `20` and `NaN`C · `20` and `63`D · `NaN` and `63`
Answer →

What's the output?

3 options
+true;
!'Lydia';
A · `1` and `false`B · `false` and `NaN`C · `false` and `false`
Answer →

Which one is true?

4 options
const bird = {
  size: 'small',
};

const mouse = {
  name: 'Mickey',
…
A · `mouse.bird.size` is not validB · `mouse[bird.size]` is not validC · `mouse[bird["size"]]` is not validD · All of them are valid
Answer →

What's the output?

5 options
let c = { greeting: 'Hey!' };
let d;

d = c;
c.greeting = 'Hello';
console.log(d.greeting);
A · `Hello`B · `Hey!`C · `undefined`D · `ReferenceError`
Answer →

What's the output?

4 options
let a = 3;
let b = new Number(3);
let c = 3;

console.log(a == b);
console.log(a === b);
…
A · `true` `false` `true`B · `false` `false` `true`C · `true` `false` `false`D · `false` `true` `true`
Answer →

What's the output?

4 options
class Chameleon {
  static colorChange(newColor) {
    this.newColor = newColor;
    return this.newColor;
  }

…
A · `orange`B · `purple`C · `green`D · `TypeError`
Answer →

What's the output?

3 options
let greeting;
greetign = {}; // Typo!
console.log(greetign);
A · `{}`B · `ReferenceError: greetign is not defined`C · `undefined`
Answer →

What happens when we do this?

4 options
function bark() {
  console.log('Woof!');
}

bark.animal = 'dog';
A · Nothing, this is totally fine!B · `SyntaxError`. You cannot add properties to a function this way.C · `"Woof"` gets logged.D · `ReferenceError`
Answer →

What's the output?

4 options
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person('Lydia', 'Hallie');
…
A · `TypeError`B · `SyntaxError`C · `Lydia Hallie`D · `undefined` `undefined`
Answer →

What's the output?

4 options
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const lydia = new Person('Lydia', 'Hallie');
…
A · `Person {firstName: "Lydia", lastName: "Hallie"}` and `undefined`B · `Person {firstName: "Lydia", lastName: "Hallie"}` and `Person {firstName: "Sarah", lastName: "Smith"}`C · `Person {firstName: "Lydia", lastName: "Hallie"}` and `{}`D · `Person {firstName: "Lydia", lastName: "Hallie"}` and `ReferenceError`
Answer →