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?
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`
What's the output?
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`
What's the output?
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`
What's the output?
+true; !'Lydia';
A · `1` and `false`B · `false` and `NaN`C · `false` and `false`
Which one is true?
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
What's the output?
let c = { greeting: 'Hey!' };
let d;
d = c;
c.greeting = 'Hello';
console.log(d.greeting);A · `Hello`B · `Hey!`C · `undefined`D · `ReferenceError`
What's the output?
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`
What's the output?
class Chameleon {
static colorChange(newColor) {
this.newColor = newColor;
return this.newColor;
}
…A · `orange`B · `purple`C · `green`D · `TypeError`
What's the output?
let greeting;
greetign = {}; // Typo!
console.log(greetign);A · `{}`B · `ReferenceError: greetign is not defined`C · `undefined`
What happens when we do this?
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`
What's the output?
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`
What's the output?
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`