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`?
undefinedmeans a variable has been declared but not assigned a value;nullis 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.
vardeclarations are hoisted and initialised toundefined;letandconstare hoisted but remain uninitialised until their declaration line runs. - What is the difference between `var`, `let` and `const`?
varis function-scoped;letandconstare block-scoped.constadditionally 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/constvariable'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,
thisdepends on how the function is called (its calling object). An arrow function does not have its ownthis; it uses thethisof its surrounding scope. - What is the difference between `call`, `apply` and `bind`? All three let you set what
thisrefers to inside a function.callandapplyinvoke the function immediately (with arguments passed individually or as an array);bindreturns a new function withthispermanently 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
setTimeoutcallbacks). - 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
asyncfunction always returns a promise, andawaitpauses execution within that function until the awaited promise settles. - What is the difference between `Promise.all` and `Promise.allSettled`?
Promise.allrejects immediately if any promise rejects;Promise.allSettledwaits for every promise and reports each one's outcome, whether fulfilled or rejected.
Arrays and objects
- What is the difference between `map` and `forEach`?
mapreturns a new array built from the return value of the callback on each item;forEachruns the callback for its side effects and always returnsundefined. - 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 }, orObject.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
- Be able to trace the output order of a snippet that mixes synchronous code, a
setTimeoutand a promise, out loud. - Write a small closure (like a counter) from memory.
- Practise with the JavaScript Fundamentals quiz and the Async JavaScript quiz.
- 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.