JavaScript interview questions, with clear answers
JavaScript interviews test whether you understand how the language actually runs — scope, closures, this and the event loop — far more than whether you remember method names.
These are the questions that come up most in frontend and full-stack rounds, each with a direct answer and a small example. If you're also preparing React, the React questions build on these.
Sample answers are examples written by Interview Fury. Adapt them to your own experience and say them in your own words.
16 questions on this page
1.What is the difference between var, let and const?
Why they ask: It's the standard opener, and it leads straight into scope and hoisting.
var is function-scoped and hoisted with the value undefined, so it can be used before the line that declares it, and it ignores block boundaries such as if and for. let and const are block-scoped; they're hoisted into a "temporal dead zone", so using them before their declaration throws a ReferenceError.
const can't be reassigned, but an object or array it holds can still be changed. Default to const, use let when you need to reassign, and avoid var.
2.What is a closure?
Why they ask: Closures are behind callbacks, private state and many interview puzzles.
A closure is a function that remembers the variables from the scope where it was created, even after that scope has finished running.
function makeCounter() {
let count = 0
return () => ++count
}
const next = makeCounter()
next() // 1
next() // 2
count stays alive because the returned function still refers to it. Closures power private state, callbacks and function factories.
3.What is hoisting?
Before code runs, declarations are processed and effectively moved to the top of their scope. Function declarations are hoisted completely, so you can call them before they appear. var declarations are hoisted but initialised to undefined. let, const and class are hoisted but not initialised, so touching them early throws an error.
console.log(a) // undefined
var a = 1
console.log(b) // ReferenceError
let b = 2
4.What is the difference between == and ===?
=== (strict equality) compares value and type, with no conversion. == converts types first, which produces surprises: 0 == '' is true, '1' == 1 is true and null == undefined is true.
Use === everywhere. The one common exception is x == null, a short way to check for both null and undefined.
5.How does this work in JavaScript?
this depends on how a function is called, not where it's written:
- As a method,
obj.fn():thisisobj. - As a plain call,
fn():undefinedin strict mode (the global object otherwise). - With
new: the newly created object. - With
call,applyorbind: whatever you pass in. - Arrow functions have no
thisof their own; they use thethisof the surrounding code, which is why they're handy inside callbacks.
Practise saying it, not just reading it
Rehearse these answers out loud with real-time AI help, tailored to your resume and the job description. Free minutes every day — no card required.
Try it free6.What is the event loop?
Why they ask: It explains asynchronous behaviour, and the output-order puzzle is a favourite.
JavaScript runs on a single thread with one call stack. Slow operations — timers, network requests — are handed to the browser or Node.js, and their callbacks are queued when they finish. The event loop moves queued callbacks onto the stack whenever it's empty. Microtasks, such as promise callbacks, run before the next macrotask, such as a setTimeout callback.
console.log('A')
setTimeout(() => console.log('B'), 0)
Promise.resolve().then(() => console.log('C'))
console.log('D')
// A, D, C, B
7.What is a Promise?
A Promise represents a value that will be available later. It starts pending, then is either fulfilled with a value or rejected with an error. You react with .then(), .catch() and .finally(), chaining steps instead of nesting callbacks.
Useful helpers: Promise.all waits for all and fails as soon as one rejects; Promise.allSettled waits for all and reports each result; Promise.race settles with the first to finish; and Promise.any resolves with the first to succeed.
8.How do async and await work?
async makes a function return a Promise. await pauses that function until a Promise settles, without blocking the main thread, so asynchronous code reads like ordinary step-by-step code:
async function loadUser(id) {
try {
const res = await fetch(`/api/users/${id}`)
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return await res.json()
} catch (err) {
console.error('Could not load the user', err)
return null
}
}
Errors are handled with try and catch. To run independent requests in parallel, start them together and await Promise.all([...]) rather than awaiting each one in turn.
9.What is the difference between null and undefined?
undefined means no value has been assigned: a declared variable without a value, a missing object property, or a function that returns nothing. null is a deliberate "no value" that you assign yourself.
typeof undefined is 'undefined', while typeof null is 'object' — a long-standing quirk of the language. The two are loosely equal (null == undefined) but not strictly equal.
10.How are arrow functions different from regular functions?
Arrow functions are a shorter syntax, like const double = n => n * 2. More importantly, they don't have their own this, arguments or super — they use the ones from the surrounding scope — they can't be called with new, and they have no prototype. That makes them ideal for callbacks, and a poor choice for object methods that need this.
11.What is prototypal inheritance?
Every JavaScript object has an internal link to another object, its prototype. When you read a property the object doesn't have, JavaScript looks along this prototype chain until it finds it or reaches null.
Classes (class Dog extends Animal) are a cleaner syntax on top of the same mechanism: methods live on Animal.prototype, and every instance shares them through the chain instead of holding its own copy.
12.What is event delegation?
Instead of adding a listener to every child element, you add one listener to a common parent and use event.target to work out which child was clicked. It works because events bubble up the DOM.
document.querySelector('#list').addEventListener('click', (e) => {
const item = e.target.closest('li')
if (item) console.log('Clicked', item.dataset.id)
})
It saves memory, and it automatically covers elements added to the list later.
13.What is the difference between debouncing and throttling?
Both limit how often a function runs. Debouncing waits until the events have stopped for a set time and then runs once — good for search-as-you-type, where you call the API 300 ms after the user stops typing. Throttling runs the function at most once per interval while the events keep coming — good for scroll and resize handlers. Both protect the browser and the server from floods of calls.
14.What is the difference between map, filter and reduce?
All three take a callback and leave the original array unchanged. map transforms every element into a new array of the same length; filter keeps the elements for which the callback returns true; reduce combines all the elements into a single value.
const prices = [100, 250, 40]
prices.map(p => p * 1.18) // add 18% tax
prices.filter(p => p > 50) // [100, 250]
prices.reduce((sum, p) => sum + p, 0) // 390
15.What are the spread and rest operators?
Both are written .... Spread expands an iterable or object: [...a, ...b] merges two arrays, and { ...user, name: 'Asha' } copies an object with one change (a shallow copy). Rest gathers the remaining items: function sum(...nums) collects the arguments into an array, and const { id, ...others } = user collects the other properties.
16.What is the difference between localStorage, sessionStorage and cookies?
localStorage keeps data for a site with no expiry until it's cleared; sessionStorage keeps it only for the current tab's session. Both hold a few megabytes, can only be read by JavaScript and are never sent to the server automatically.
Cookies are small (about 4 KB), are sent with every request to their domain, can expire, and can be marked HttpOnly (unreadable by JavaScript) and Secure. That's why login sessions usually live in HttpOnly cookies rather than in localStorage.
Prepare with Interview Fury
Rehearse these answers out loud with real-time AI help, tailored to your resume and the job description. Free minutes every day — no card required.
Try it free