Interview questions

JavaScript Interview Questions

20 JavaScript interview questions with model answers: closures, this, hoisting, the event loop, promises and Express middleware. For both sides.

JavaScript Interview Questions

What's here

20 JavaScript interview questions, grouped into 4 areas: language fundamentals, objects, prototypes and functions, asynchronous javascript and browser, node.js and practical questions. Each one comes with why it is worth asking and what a strong answer contains, so the same question can be scored the same way by different interviewers.

  • Ask candidates to predict the output of short snippets (event loop order, closures in loops, 'this' in callbacks); it tests understanding quickly.
  • Pair one live-coding task, such as debounce or groupBy, with a conversation about trade-offs.
  • Follow 'what is a closure?' with 'where did you last rely on one?' to separate memorised definitions from practice.
For hiring teams

Cohesyve finds, contacts and tests JavaScript candidates on the real work, then ranks them for these interviews. 14-day free trial for 10 candidates, not charged until it ends.

Preparing for this interview yourself? Practise it free →

These JavaScript interview questions cover the language features every JS interview returns to: scope and hoisting, closures, 'this', prototypes, the event loop and async code, plus browser and Node.js practicalities such as event delegation, modules and Express middleware. Candidates can use the 'what to look for' notes as model answers; interviewers can use them as a scoring guide and a source of follow-up questions.

Language Fundamentals

Scope, equality and functions: the areas where JavaScript behaves differently from other languages.

  1. 1

    What are the differences between var, let and const?

    Why ask it

    Scoping bugs are among the most common in JavaScript.

    What to look for

    var is function-scoped and can be read as undefined before its declaration; let and const are block-scoped and sit in a temporal dead zone until declared, so early access throws a ReferenceError. const prevents rebinding, not mutation: a const object's properties can still change. Top-level let does not create a property on the global object.
  2. 2

    What is the difference between == and ===?

    Why ask it

    A basic question that reveals whether the candidate understands type coercion.

    What to look for

    === compares without type conversion; == converts types first, which produces surprising results such as '0' == 0 being true. Use === by default. Some teams allow 'x == null' as a deliberate check for both null and undefined.
  3. 3

    What is hoisting?

    Why ask it

    Explains a lot of confusing behaviour in older code.

    What to look for

    Declarations are processed before code runs. Function declarations are fully available before they appear; var declarations exist but are undefined until assigned; let, const and class bindings exist but cannot be accessed until their declaration runs (the temporal dead zone).
  4. 4

    Explain closures and give a practical example.

    Why ask it

    Closures underpin callbacks, module patterns and React hooks.

    What to look for

    A function keeps access to variables from the scope where it was created, even after that scope has returned. Examples: a counter factory with private state, event handlers that remember an ID, debounce functions. Strong answers mention the classic loop bug with var and setTimeout, and that let fixes it by creating a new binding each iteration.
  5. 5

    How is the value of 'this' decided?

    Why ask it

    'this' confuses many developers coming from class-based languages.

    What to look for

    It depends on how a function is called: the object before the dot for a method call, undefined in strict mode for a plain call, the new instance with 'new', and an explicit value with call, apply or bind. Arrow functions take 'this' from the surrounding scope and ignore how they are called.
For hiring teams

Before the JavaScript interviews, see who can do the work

Cohesyve finds and contacts JavaScript candidates from your job description and tests each one on an AI job simulation of the role, so these questions go to a ranked shortlist. 14-day free trial for 10 candidates, not charged until it ends.

Objects, Prototypes and Functions

How JavaScript models objects and data, and how fluent the candidate is with functional patterns.

  1. 6

    How does prototypal inheritance work, and how do classes relate to it?

    Why ask it

    Shows depth beyond everyday syntax.

    What to look for

    Objects link to a prototype object; property lookups walk up the prototype chain until found or the chain ends. Class syntax is a cleaner way to set up constructor functions and prototypes, not a different inheritance model.
  2. 7

    How do arrow functions differ from regular functions?

    Why ask it

    Choosing the wrong one causes 'this' bugs.

    What to look for

    Arrow functions have no own 'this', 'arguments' or 'super', cannot be used as constructors (calling them with 'new' throws a TypeError) and have no prototype property. They suit callbacks; regular functions or methods suit object methods that need their own 'this'.
  3. 8

    How do you copy an object, and what is the difference between a shallow and a deep copy?

    Why ask it

    Accidental shared references cause state bugs, especially in front-end frameworks.

    What to look for

    Spread syntax and Object.assign make shallow copies: nested objects are still shared. structuredClone makes a deep copy, keeps circular references and throws for values it cannot clone. JSON round-tripping loses dates, undefined and other types, so it is a weak choice.
  4. 9

    When would you use map, filter and reduce, and when is a plain loop clearer?

    Why ask it

    Tests fluency and judgement.

    What to look for

    map transforms each item, filter keeps matching items, reduce folds a list into one value. They return new arrays and avoid mutation. A plain loop can be clearer for complex logic, early exits or performance-sensitive code; an over-clever reduce is a readability smell.
  5. 10

    What is the difference between primitives and objects in how they are stored and compared?

    Why ask it

    Underlies equality checks and immutability.

    What to look for

    Primitives (string, number, boolean, null, undefined, symbol, bigint) are compared by value and are immutable. Objects, including arrays and functions, are compared by reference, so two identical-looking objects are not equal. This is why frameworks rely on new references to detect changes.

Asynchronous JavaScript

The event loop and promises appear in almost every JavaScript interview.

  1. 11

    Explain the event loop. What does this print: setTimeout(log 'timeout', 0), Promise.resolve().then(log 'promise'), log 'sync'?

    Why ask it

    Predicting order shows real understanding of microtasks and tasks.

    What to look for

    'sync', then 'promise', then 'timeout'. Synchronous code runs to completion first; promise callbacks are microtasks, and the microtask queue is drained before the next task; setTimeout callbacks are tasks. Each job runs to completion without interruption.
  2. 12

    How do promises and async/await relate, and how do you handle errors with each?

    Why ask it

    Error handling in async code is often done badly.

    What to look for

    async/await is syntax over promises: an async function returns a promise and await pauses until a promise settles. Errors are handled with .catch on promises or try/catch around await. Strong answers mention unhandled rejections and not forgetting to await or return a promise.
  3. 13

    What is the difference between Promise.all, Promise.allSettled, Promise.any and Promise.race?

    Why ask it

    Running work in parallel correctly is a practical skill.

    What to look for

    all fulfils when every promise fulfils and rejects on the first rejection; allSettled waits for all and never rejects, reporting each outcome; any fulfils with the first fulfilment and rejects only if all reject (with an AggregateError); race settles the same way as whichever promise settles first.
  4. 14

    What is the difference between debouncing and throttling? Write a debounce function.

    Why ask it

    A common live-coding task that uses closures and timers.

    What to look for

    Debounce waits until calls stop for a set delay before running once (search inputs); throttle runs at most once per interval (scroll handlers). A debounce function keeps a timer ID in a closure, clears it on each call and sets a new timeout that calls the original function with the latest arguments and 'this'.
  5. 15

    How do you keep heavy work from freezing the page?

    Why ask it

    Single-threaded execution is a real constraint in browsers.

    What to look for

    Long synchronous work blocks rendering and input. Options: break work into chunks that yield back to the event loop, move CPU-heavy work to a Web Worker, use asynchronous APIs, and avoid unnecessary work in hot paths. Measure with browser performance tools first.

Browser, Node.js and Practical Questions

Where JavaScript meets the DOM, the server and real-world security.

  1. 16

    What are event bubbling and event delegation?

    Why ask it

    Basic DOM knowledge for any front-end role.

    What to look for

    Events fire on the target and then bubble up through its ancestors. Delegation attaches one listener to a parent and uses event.target to handle events from many children, including ones added later. Good answers mention stopPropagation and capture phase at a basic level.
  2. 17

    What is the difference between ES modules and CommonJS?

    Why ask it

    Module systems affect tooling, Node.js projects and bundling.

    What to look for

    ES modules use import and export, are statically analysable (enabling tree-shaking) and are the standard in browsers and modern Node.js. CommonJS uses require and module.exports and loads synchronously, and is common in older Node.js code. Mixing them needs care.
  3. 18

    What is middleware in Express, and how does error-handling middleware differ?

    Why ask it

    Express is a common Node.js framework in JavaScript interviews.

    What to look for

    Middleware functions receive req, res and next; each either ends the request-response cycle or calls next() to pass control on, otherwise the request hangs. Error-handling middleware has four arguments (err, req, res, next), and Express identifies it by that signature, so all four must be declared.
  4. 19

    What security issues do you watch for in JavaScript applications?

    Why ask it

    Front-end and Node.js code are both common attack surfaces.

    What to look for

    Cross-site scripting from inserting untrusted HTML (innerHTML, unsafe rendering), eval and similar dynamic code, storing tokens where scripts can read them, unvalidated input on the server, and vulnerable or malicious dependencies. Mitigations include escaping by default, a content security policy, input validation and dependency auditing.
  5. 20

    Write a function that groups an array of objects by a given key.

    Why ask it

    A short task that shows fluency with objects, loops and edge cases.

    What to look for

    A loop or reduce that builds an object (or a Map) of arrays keyed by item[key], creating each array on first sight. Strong candidates ask about missing keys and non-string keys, choose a Map where keys are not strings, and may mention built-in grouping helpers where the runtime supports them.

Running the interview well

  • 1Ask candidates to predict the output of short snippets (event loop order, closures in loops, 'this' in callbacks); it tests understanding quickly.
  • 2Pair one live-coding task, such as debounce or groupBy, with a conversation about trade-offs.
  • 3Follow 'what is a closure?' with 'where did you last rely on one?' to separate memorised definitions from practice.
  • 4Match the second half of the interview to the role: DOM and browser APIs for front-end, Node.js and Express for backend.
  • 5Do not penalise candidates for looking up exact method names; score reasoning.
  • 6For senior roles, discuss code organisation, testing, performance and security rather than trivia.

Common questions

What JavaScript topics should I revise for an interview?

Scope and hoisting, closures, 'this', prototypes, equality and type coercion, array methods, the event loop, promises and async/await, and DOM events. Backend interviews add Node.js modules and Express.

How do I prepare for JS interview questions?

Write small programs that use each concept, predict the output of tricky snippets before running them, and practise common live-coding tasks such as debounce, deep clone, flattening arrays and grouping data. Explain your reasoning out loud as you go.

Is TypeScript asked in JavaScript interviews?

Often, because many teams use it. Expect basic questions on types, interfaces, generics and how TypeScript catches errors before runtime. The JavaScript fundamentals still come first.

Are Express JS interview questions part of JavaScript interviews?

For backend and full-stack roles, yes. Expect middleware, routing, error handling, request validation and how Express fits with databases and authentication.

What is the hardest JavaScript interview topic?

Most candidates find the event loop, 'this' and closures hardest, because they require a clear mental model rather than memorisation. Practising output-prediction exercises is the quickest way to build that model.

Do I need to know old JavaScript patterns?

Know enough to read them: var, callbacks, prototype-based classes and CommonJS still appear in legacy code. Write new code with let and const, promises or async/await, classes or functions, and ES modules.

For hiring teams

Save these questions for the JavaScript candidates who can do the work

Cohesyve takes your JavaScript job description, finds candidates, reaches out to them and runs an AI job simulation of the real work, then returns a ranked shortlist.

1,500+

assessments completed

50%

faster time-to-hire

5 min

from JD to assessment

14-day free trial · 10 candidates · Not charged until the trial ends

For job seekers

Preparing for a JavaScript interview yourself? Practise on an AI job simulation of the role. 3-day free trial, cancel anytime.

Practise a JavaScript interview free →

See Cohesyve in action

Free 30-min walkthrough

See it on your role