JavaScript interview questions

Scope, hoisting, closures, this, the event loop, promises, array methods, copying, and the output-prediction questions every JS interview includes.

Readiness: 0 of 46 nailed

How to use this page: 1. Read a question2. Type your answer3. Check with AI4. Open the model answer5. Timed drill when you’re 70% ready

Scope

FoundationConcept

What is the difference between var, let and const?

JuniorPredict the output

What does this print?

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

What happens here?

console.log(greet());
console.log(sayHi());

function greet() { return 'hello'; }
const sayHi = () => 'hi';
Mid-levelPredict the output

What does this print?

var foo = 1;
function foo() {}
console.log(typeof foo);

Functions

JuniorConcept

What is a closure? Give a real use.

Mid-levelWrite it by hand

Write a debounce function by hand.

Mid-levelPredict the output

What does this print?

const user = {
  name: 'Ali',
  regular() { return this.name; },
  arrow: () => this?.name,
};
const fn = user.regular;
console.log(user.regular(), user.arrow(), fn());
Mid-levelPredict the output

What does this print?

'use strict';
const team = {
  name: 'Team',
  print() {
    [1].forEach(function () { console.log(this?.name); });
    [1].forEach(() => console.log(this.name));
  },
};
team.print();
JuniorConcept

What is the difference between call, apply and bind?

SeniorWrite it by hand

Implement Function.prototype.bind yourself (simplified).

Mid-levelWrite it by hand

Write a throttle function.

Mid-levelWrite it by hand

What is currying? Implement curry for a function with fixed arguments.

Async

Mid-levelPredict the output

In what order are these logged?

console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');
JuniorIt’s broken: why?

Why does this log undefined?

function getUser() {
  fetch('/api/user').then(res => res.json());
}
const user = getUser();
console.log(user);
Mid-levelReal-world scenario

You need data from three independent APIs. How do you fetch them efficiently?

Mid-levelPredict the output

In what order is this logged?

console.log(1);
setTimeout(() => console.log(5), 0);
async function run() {
  console.log(2);
  await null;
  console.log(3);
}
run();
console.log(4);
SeniorWrite it by hand

Implement Promise.all yourself.

Mid-levelWrite it by hand

Add a timeout to a fetch request.

JuniorConcept

Explain the event loop in your own words.

Arrays and objects

FoundationWrite it by hand

Given orders, get the total revenue of paid orders using filter, map and reduce.

JuniorPredict the output

What does this print?

const original = { name: 'Ali', address: { city: 'Lahore' } };
const copy = { ...original };
copy.name = 'Sara';
copy.address.city = 'Karachi';
console.log(original.name, original.address.city);
Mid-levelPredict the output

What does this print?

const obj = { b: 1, 2: 1, a: 1, 1: 1 };
console.log(Object.keys(obj));
Mid-levelWrite it by hand

Write a function to deep-compare two plain objects or arrays.

JuniorWrite it by hand

Flatten a nested array like [1, [2, [3, [4]]]] without .flat().

JuniorConcept

When would you use a Map instead of a plain object?

JuniorWrite it by hand

Update a nested property without mutating the original object.

JuniorConcept

What’s wrong with JSON.parse(JSON.stringify(obj)) for deep copying?

Types

JuniorPredict the output

Which of these is true?

typeof null === 'object'
NaN === NaN
0.1 + 0.2 === 0.3
[] === []
FoundationPredict the output

How many of these values are falsy: 0, '0', '', [], null, NaN, {}?

JuniorReal-world scenario

How should you handle money in JavaScript?

DOM

Mid-levelConcept

What is event delegation? Why use it?

FoundationConcept

What are event bubbling and capturing? What does stopPropagation do?

Browser

Mid-levelIt’s broken: why?

“Blocked by CORS policy.” What does it mean and who fixes it?

FoundationConcept

localStorage vs sessionStorage vs cookies?

Mid-levelConcept

What is a CORS preflight request, and when does it happen?

Performance

SeniorIt’s broken: why?

A single-page app gets slower and uses more memory the longer it’s open. What would you look for?

JuniorWrite it by hand

Write a memoize function.

Mid-levelConcept

What are Web Workers and when would you use one?

Mid-levelConcept

How does garbage collection work in JavaScript?

Objects

JuniorConcept

What is prototypal inheritance? How do classes relate to it?

Mid-levelConcept

How do you make truly private fields in a JavaScript class?

Language

Mid-levelConcept

What are iterators and generators in JavaScript?

JuniorConcept

CommonJS vs ES modules?

FoundationConcept

What do ?. and ?? do?

JuniorIt’s broken: why?

Your form validation regex rejects valid emails like ali+work@company.co.uk. What’s your approach?

Errors

FoundationIt’s broken: why?

TypeError: data.map is not a function. What’s the likely cause?

Also prepare