JavaScript interview questions & answers
JavaScript is a dynamically typed, single-threaded, prototype-based language that powers interactivity on the web and, through Node.js, the server. It runs on an event loop that handles asynchronous operations without blocking, and it treats functions as first-class values. Interviews emphasize closures, scope, the event loop, promises, and prototypal inheritance.
Updated 2026-06-18 · 15 real, commonly-asked questions with answers.
Key takeaways
- JavaScript is a dynamically typed, single-threaded, prototype-based language that powers interactivity on the web and, through Node.js, the server.
- Core areas to revise for JavaScript: Scope & closures, Hoisting & this binding, Event loop & async, Promises & async/await, Prototypes & inheritance.
- This guide answers 15 of the most-asked JavaScript interview questions — rehearse them in OnJob's free AI mock interview.
Top 15 JavaScript interview questions
Q1.What is the difference between var, let, and const?
var is function-scoped and hoisted with an initial value of undefined, which can cause bugs. let and const are block-scoped and live in a temporal dead zone until declared. const must be initialized and cannot be reassigned, though objects it points to can still be mutated.
Q2.What is a closure?
A closure is a function bundled together with references to its surrounding lexical scope, so it can access variables from an outer function even after that function has returned. This enables data privacy and stateful functions like counters. Closures are central to callbacks, currying, and module patterns.
Q3.Explain the difference between == and ===.
The double-equals is loose equality that performs type coercion before comparing, so 0 equals the string zero is true. The triple-equals is strict equality that compares both value and type with no coercion, so that same comparison is false. Use strict equality by default to avoid surprising coercion bugs.
Q4.What is hoisting?
Hoisting is JavaScript's behavior of moving declarations to the top of their scope during compilation. Function declarations and var variables are hoisted (var initialized to undefined), so they can be referenced before their line. let and const are hoisted but remain in the temporal dead zone until evaluated, throwing if accessed early.
Q5.Explain the event loop.
JavaScript runs on a single call stack, and the event loop coordinates asynchronous work. When the stack is empty, the loop pulls callbacks from the microtask queue (promises) first, then the macrotask queue (timers, I/O), and pushes them onto the stack. This lets non-blocking async code run despite a single thread.
Q6.What is the difference between null and undefined?
undefined means a variable has been declared but not assigned a value, and it is the default for missing function returns or parameters. null is an intentional assignment representing the absence of a value. The typeof undefined is undefined, while typeof null is the historical quirk object.
Q7.What are promises and how do they work?
A promise is an object representing the eventual result of an asynchronous operation, with states pending, fulfilled, or rejected. You attach handlers with then for success and catch for errors, and you can chain them. Promises avoid deeply nested callbacks and underpin async/await.
Q8.What is async/await?
async/await is syntactic sugar over promises that lets you write asynchronous code that reads like synchronous code. An async function always returns a promise, and await pauses execution until a promise settles, returning its value. Errors are handled naturally with try/catch around await.
Q9.How does the this keyword work?
The value of this depends on how a function is called. In a regular function it is the calling object, or undefined or global when called plainly. Arrow functions do not bind their own this; they inherit it from the enclosing scope. You can also set this explicitly with call, apply, or bind.
Q10.What is prototypal inheritance?
JavaScript objects link to a prototype object, and property lookups that fail on the object travel up the prototype chain. This lets objects share methods and properties without classical classes. The class syntax introduced in ES6 is largely syntactic sugar over this prototype mechanism.
Q11.What is the difference between map, filter, and reduce?
map transforms each element and returns a new array of the same length. filter returns a new array containing only elements that pass a test. reduce accumulates the array down to a single value using an accumulator function, useful for sums, grouping, or flattening.
Q12.What is event delegation?
Event delegation attaches a single listener to a common parent element instead of many listeners on individual children. It works because events bubble up the DOM tree, so the parent can inspect the event target to identify which child triggered it. This improves performance and handles dynamically added elements automatically.
Q13.Explain the difference between call, apply, and bind.
call invokes a function immediately with a given this and arguments passed individually. apply does the same but takes arguments as an array. bind does not invoke; it returns a new function permanently bound to the given this for later calls.
Q14.What are higher-order functions?
A higher-order function is one that takes another function as an argument, returns a function, or both. They enable abstraction and composition, and JavaScript treats functions as first-class values to support them. Array methods like map, filter, and reduce, plus callbacks, are common examples.
Q15.What is debouncing and throttling?
Debouncing delays running a function until a pause of a specified length occurs after the last call, ideal for search-as-you-type inputs. Throttling guarantees a function runs at most once per time interval no matter how often it is triggered, ideal for scroll or resize handlers. Both limit how often expensive handlers fire.
More interview topics
Practise & prepare
Practise JavaScript out loud
Reading answers is step one. Rehearse them in OnJob's free AI mock interview, get instant feedback, then apply to AI-matched jobs in one click.