Development

JavaScript Interview Questions and Answers

Common JavaScript interview questions on closures, hoisting, promises, the event loop and array methods, answered concisely so you can explain them confidently.

QuizMaster TeamSeptember 27, 202610 min read

JavaScript interviews tend to circle back to the same handful of topics: scope and closures, asynchronous behaviour, and a handful of array and object idioms. This page answers the questions directly, in the concise form an interviewer is listening for, so you can check your understanding before the real thing.

Language basics

  • What are the primitive data types in JavaScript? string, number, boolean, undefined, null, symbol and bigint. Everything else, including arrays and functions, is an object.
  • What is the difference between `null` and `undefined`? undefined means a variable has been declared but not assigned a value; null is an intentional, explicit "no value", assigned by the programmer.
  • What is the difference between `==` and `===`? == compares after converting both operands to a common type; === compares type and value with no conversion, and should be the default choice.
  • What is NaN, and how do you check for it? "Not a Number", the result of an invalid numeric operation. Check with Number.isNaN(value), since NaN is the only value not equal to itself.

Scope, hoisting and closures

  • What is hoisting? JavaScript moves variable and function declarations to the top of their scope during compilation. var declarations are hoisted and initialised to undefined; let and const are hoisted but remain uninitialised until their declaration line runs.
  • What is the difference between `var`, `let` and `const`? var is function-scoped; let and const are block-scoped. const additionally prevents the variable itself from being reassigned.
  • What is a closure? A function that retains access to variables from its enclosing scope even after that outer function has returned, commonly used for data privacy and factory-style counters.
  • What is the temporal dead zone? The period between entering a scope and a let/const variable's declaration line being executed, during which accessing the variable throws a ReferenceError.

Functions and this

  • How does `this` behave in a regular function versus an arrow function? In a regular function, this depends on how the function is called (its calling object). An arrow function does not have its own this; it uses the this of its surrounding scope.
  • What is the difference between `call`, `apply` and `bind`? All three let you set what this refers to inside a function. call and apply invoke the function immediately (with arguments passed individually or as an array); bind returns a new function with this permanently set, without calling it.
  • What is an IIFE? An Immediately Invoked Function Expression: a function defined and called at the same time, often used to create an isolated scope.

Asynchronous JavaScript

  • What is the event loop? The mechanism that lets JavaScript, a single-threaded language, handle asynchronous operations: synchronous code runs first, then queued microtasks (like promise callbacks), then macrotasks (like setTimeout callbacks).
  • What is a Promise? An object representing the eventual result of an asynchronous operation, which can be pending, fulfilled or rejected.
  • What does `async`/`await` do? Syntax that lets asynchronous, promise-based code be written and read like synchronous code. An async function always returns a promise, and await pauses execution within that function until the awaited promise settles.
  • What is the difference between `Promise.all` and `Promise.allSettled`? Promise.all rejects immediately if any promise rejects; Promise.allSettled waits for every promise and reports each one's outcome, whether fulfilled or rejected.

Arrays and objects

  • What is the difference between `map` and `forEach`? map returns a new array built from the return value of the callback on each item; forEach runs the callback for its side effects and always returns undefined.
  • What does `filter` do? Returns a new array containing only the elements for which the callback returns true.
  • How do you make a shallow copy of an object? With the spread operator, { ...original }, or Object.assign({}, original). Nested objects are still shared, not deeply copied.
  • What is destructuring? A syntax for unpacking values from arrays or properties from objects into distinct variables in one statement, e.g. const { name, age } = user;.

How to prepare

  1. Be able to trace the output order of a snippet that mixes synchronous code, a setTimeout and a promise, out loud.
  2. Write a small closure (like a counter) from memory.
  3. Practise with the JavaScript Fundamentals quiz and the Async JavaScript quiz.
  4. Review the JavaScript quiz preparation guide for the coercion traps interviewers love to probe.

Key takeaways

  • Master closures, hoisting and the difference between var, let and const; they come up constantly.
  • Be able to explain the event loop's order: synchronous code, then microtasks, then macrotasks.
  • Know call, apply and bind, and how this differs between regular and arrow functions.
  • Practise tracing asynchronous code by hand before the interview, not during it.