50 JavaScript Interview Questions and Answers for Developers in 2026

AI Summary
Get a quick overview of the key points
Tap Generate Magic to let AI read for you.
Preparing for a JavaScript, React, or Node.js interview? Here are 50 commonly asked questions explained in simple English, with short examples and useful resources.
JavaScript interviews are not only about writing code. Interviewers often want to know whether you understand how JavaScript actually works.
Questions about var, let, closures, promises, async/await, the event loop, modules, and objects are common because they show how well you understand the language.
So, instead of memorizing complicated definitions, let's understand these questions in the simplest way possible.
1. What is the difference between var, let, and const?
This is one of the first JavaScript questions many interviewers ask.
The main difference is scope and reassignment.
js1var name = "Sahil"; 2let age = 21; 3const country = "India";
varis function-scoped.letis block-scoped.constis also block-scoped.letcan be reassigned.constcannot be reassigned.
js1let age = 21; 2age = 22; // ✅ 3 4const country = "India"; 5country = "USA"; // ❌
Simple rule: Prefer const by default. Use let when the value needs to change. Avoid var in modern JavaScript.
📚 MDN — Variables and declarations
2. What is hoisting?
Hoisting means JavaScript processes certain declarations before running the code.
For example:
js1console.log(name); 2 3var name = "Sahil";
This doesn't immediately throw an error because var is hoisted and initialized with undefined.
Conceptually, it behaves somewhat like:
js1var name; 2 3console.log(name); 4 5name = "Sahil";
However, let and const behave differently because of the Temporal Dead Zone (TDZ).
3. What is the Temporal Dead Zone?
The Temporal Dead Zone is the time between entering a block and reaching the declaration of a let or const variable.
js1console.log(name); // ❌ ReferenceError 2 3let name = "Sahil";
The variable exists in the scope, but you cannot access it before its declaration.
Easy way to remember:
let and const are hoisted, but they cannot be used before their declaration.
4. What is the difference between == and ===?
== compares values after performing type conversion.
=== compares both value and type.
js15 == "5" // true 25 === "5" // false
In modern JavaScript, === is generally preferred because it avoids unexpected type conversion.
5. What are primitive data types in JavaScript?
JavaScript has seven primitive types:
- String
- Number
- BigInt
- Boolean
- Undefined
- Symbol
- Null
Example:
js1let name = "Sahil"; // String 2let age = 21; // Number 3let active = true; // Boolean 4let value = undefined; // Undefined 5let data = null; // Null
Objects, arrays, and functions are not primitive values.
Objects and Copying
6. What is the difference between shallow copy and deep copy?
This is a very common interview question.
Suppose we have:
js1const user = { 2 name: "Sahil", 3 address: { 4 city: "Lucknow" 5 } 6};
A shallow copy copies only the first level.
js1const copy = { ...user };
But address is still shared between both objects.
A deep copy creates independent nested objects too.
js1const copy = structuredClone(user);
Now changing:
js1copy.address.city = "Delhi";
doesn't change the original object.
Remember:
Shallow copy → nested references are shared Deep copy → nested data is copied too
7. How can you create a shallow copy of an object?
Two common ways are:
js1const copy = { ...user };
or:
js1const copy = Object.assign({}, user);
Both create a shallow copy.
8. What is destructuring?
Destructuring lets you take values from an object or array and store them in variables.
Without destructuring:
js1const user = { 2 name: "Sahil", 3 age: 21 4}; 5 6const name = user.name; 7const age = user.age;
With destructuring:
js1const { name, age } = user;
It makes code shorter and easier to read.
9. What is the difference between map(), filter(), and reduce()?
These three array methods are extremely common in interviews.
map()
Creates a new array by changing every element.
js1const numbers = [1, 2, 3]; 2 3const result = numbers.map(n => n * 2); 4 5console.log(result); 6// [2, 4, 6]
filter()
Keeps elements that satisfy a condition.
js1const result = numbers.filter(n => n > 1); 2 3// [2, 3]
reduce()
Combines an array into a single value.
js1const result = numbers.reduce((sum, n) => sum + n, 0); 2 3// 6
Easy way to remember:
map → transform
filter → select
reduce → combine
Functions and Closures
10. What is a closure?
A closure happens when a function remembers variables from its outer scope even after the outer function has finished executing.
js1function counter() { 2 let count = 0; 3 4 return function () { 5 count++; 6 return count; 7 }; 8} 9 10const increment = counter(); 11 12console.log(increment()); // 1 13console.log(increment()); // 2
The inner function still remembers count.
Closures are commonly used for:
- Data privacy
- Counters
- Callbacks
- Function factories
- React hooks and other patterns
11. What is the difference between an arrow function and a normal function?
Arrow functions have a shorter syntax:
js1const add = (a, b) => a + b;
But the important interview difference is this.
Arrow functions do not create their own this. They use this from their surrounding scope.
Normal functions have their own this depending on how they are called.
12. What is this in JavaScript?
this refers to the object associated with the current function call.
For example:
js1const user = { 2 name: "Sahil", 3 4 sayHello() { 5 console.log(this.name); 6 } 7}; 8 9user.sayHello();
Here, this refers to user.
One important thing to remember is that this depends on how a function is called, not simply where it was created.
Promises and Async JavaScript
13. What is a Promise?
A Promise represents the result of an asynchronous operation.
For example, when you make an API request, you don't immediately know whether it will succeed or fail.
A Promise can be:
pendingfulfilledrejected
js1const promise = fetch("/api/users"); 2 3promise 4 .then(response => response.json()) 5 .catch(error => console.log(error));
14. What is async/await?
async/await is a cleaner way to work with Promises.
Instead of:
js1fetch("/api/users") 2 .then(response => response.json()) 3 .then(data => console.log(data));
You can write:
js1async function getUsers() { 2 const response = await fetch("/api/users"); 3 const data = await response.json(); 4 5 console.log(data); 6}
It makes asynchronous code easier to read.
15. Does an async function always return a Promise?
Yes.
js1async function hello() { 2 return "Hello"; 3}
Even though we return a string, JavaScript automatically wraps it in a Promise.
js1hello().then(result => { 2 console.log(result); 3});
16. Does await block JavaScript?
No.
await pauses the current async function, not the entire JavaScript runtime.
This is an important distinction.
js1async function test() { 2 const data = await fetch("/api"); 3}
Other JavaScript work can continue while the asynchronous operation is waiting.
17. What is Promise.all()?
Promise.all() lets you wait for multiple Promises together.
js1const [users, posts] = await Promise.all([ 2 fetchUsers(), 3 fetchPosts() 4]);
The operations can run concurrently.
However, if one Promise rejects, Promise.all() rejects.
18. What is the difference between Promise.all() and Promise.allSettled()?
Promise.all() fails when one Promise fails.
Promise.allSettled() waits for every Promise, regardless of success or failure.
This makes allSettled() useful when you want the result of every operation.
Event Loop
19. What is the JavaScript Event Loop?
JavaScript executes synchronous code using the call stack.
When asynchronous work is involved, the runtime schedules callbacks and the event loop helps decide when those callbacks can run.
A simplified model is:
Call Stack → Runtime APIs → Queues → Event Loop → Call Stack
This is one of the most important concepts for JavaScript and Node.js interviews.
📚 MDN — JavaScript execution model
20. What is the Call Stack?
The call stack keeps track of functions currently being executed.
js1function one() { 2 two(); 3} 4 5function two() { 6 console.log("Hello"); 7} 8 9one();
JavaScript pushes functions onto the stack and removes them when they finish.
If the stack becomes too large, you can get:
text1Maximum call stack size exceeded
21. What is a microtask?
A microtask is a high-priority asynchronous task.
Promise callbacks are a common example:
js1Promise.resolve().then(() => { 2 console.log("Microtask"); 3});
Microtasks are processed before the next task is taken from the task queue.
22. What is a macrotask?
The term "macrotask" is commonly used for tasks such as timers and event callbacks.
For example:
js1setTimeout(() => { 2 console.log("Timer"); 3}, 0);
The callback doesn't run immediately. It gets scheduled as a task.
23. Which runs first: Promise or setTimeout?
Consider:
js1console.log("A"); 2 3setTimeout(() => console.log("B"), 0); 4 5Promise.resolve().then(() => console.log("C")); 6 7console.log("D");
Output:
text1A 2D 3C 4B
Why?
- Synchronous code runs first.
- Promise callback goes to the microtask queue.
- Timer callback goes to the task queue.
- Microtasks are processed before the next task.
This is a very common output-based interview question.
Modules
24. What is the difference between import and require()?
import belongs to the ES Module system.
js1import express from "express";
require() belongs to CommonJS.
js1const express = require("express");
Modern Node.js supports both module systems, although their configuration and behavior differ.
📚 Node.js — Modules 📚 Node.js — ECMAScript Modules
25. What is the difference between default and named exports?
Named export:
js1export const name = "Sahil";
Import:
js1import { name } from "./user.js";
Default export:
js1export default function hello() {}
Import:
js1import hello from "./user.js";
A file can have multiple named exports but only one default export.
More Questions You Should Know
26. What is event bubbling?
When an event happens on a child element, it can move upward through its parent elements.
text1button 2 ↓ 3div 4 ↓ 5body 6 ↓ 7document
This is called event bubbling.
27. What is event delegation?
Instead of adding listeners to every child, you add one listener to the parent.
js1list.addEventListener("click", event => { 2 if (event.target.matches("button")) { 3 console.log("Button clicked"); 4 } 5});
It is useful when dealing with many or dynamically created elements.
28. What is debouncing?
Debouncing waits until the user stops triggering an event before running a function.
A common example is a search box.
Instead of making an API request for every keystroke:
text1J 2Ja 3Jav 4Java 5JavaS 6JavaSc...
you wait until the user stops typing.
29. What is throttling?
Throttling limits how frequently a function can execute.
For example, you might limit a scroll handler to run once every 100ms instead of hundreds of times per second.
30. What is optional chaining?
Optional chaining allows you to safely access nested properties.
Instead of:
js1user && user.address && user.address.city
you can write:
js1user?.address?.city
If something doesn't exist, JavaScript returns undefined instead of throwing an error.
31. What is the nullish coalescing operator?
The ?? operator provides a fallback when a value is null or undefined.
js1const name = user.name ?? "Guest";
Unlike ||, it doesn't treat values like 0 or false as missing.
32. What is a callback function?
A callback is simply a function passed to another function.
js1function greet(name, callback) { 2 console.log("Hello " + name); 3 callback(); 4} 5 6greet("Sahil", () => { 7 console.log("Done"); 8});
Callbacks are heavily used in asynchronous JavaScript.
33. What is a higher-order function?
A higher-order function either:
- accepts another function as an argument, or
- returns another function.
For example:
js1const numbers = [1, 2, 3]; 2 3const result = numbers.map(n => n * 2);
Here, map() is a higher-order function because it accepts a function.
34. What are call(), apply(), and bind()?
All three can control the value of this.
js1user.sayHello.call(otherUser);
The main difference:
call()→ arguments separatelyapply()→ arguments as an arraybind()→ returns a new function
35. What is a prototype?
Every JavaScript object can have access to another object called its prototype.
JavaScript uses prototypes for inheritance.
js1const user = { 2 name: "Sahil" 3}; 4 5console.log(user.toString);
You didn't define toString() yourself. It comes through the prototype chain.
📚 MDN — Inheritance and prototype chain
36. What is the prototype chain?
When JavaScript cannot find a property on an object, it looks at its prototype.
It continues looking up the chain until it finds the property or reaches null.
text1object 2 ↓ 3prototype 4 ↓ 5prototype 6 ↓ 7null
This is called the prototype chain.
37. What is the difference between Map and Object?
Both can store key-value pairs, but they are designed differently.
Map provides useful features such as:
js1const users = new Map(); 2 3users.set("id", 101); 4users.get("id");
Map supports keys of any type and has methods like set(), get(), and has().
Objects are often better when you're representing structured data.
38. What is a Set?
A Set stores unique values.
js1const numbers = new Set([1, 2, 2, 3]); 2 3console.log(numbers); 4// Set {1, 2, 3}
A common use is removing duplicates from an array:
js1const unique = [...new Set(numbers)];
39. What is memoization?
Memoization means storing the result of an expensive function so you don't have to calculate it again.
For example, if a function receives the same input multiple times, you can cache the previous result.
It's useful for optimization but should be used when the memory cost is worth the performance benefit.
40. What is currying?
Currying transforms a function with multiple arguments into a series of functions with one argument.
Instead of:
js1add(2, 3);
you might have:
js1add(2)(3);
It is a common functional-programming concept.
JavaScript + Browser
41. What is the DOM?
DOM stands for Document Object Model.
The browser converts an HTML document into a tree-like structure that JavaScript can interact with.
js1document.querySelector("#title");
JavaScript can then read or modify the page through the DOM.
42. What is CORS?
CORS stands for Cross-Origin Resource Sharing.
It is a browser security mechanism that controls whether a web page can make requests to another origin.
For example:
text1Frontend: https://myapp.com 2API: https://api.example.com
The server needs to allow the appropriate cross-origin request.
43. What is fetch()?
fetch() is a browser API used to make HTTP requests.
js1const response = await fetch("/api/users"); 2 3const users = await response.json();
It returns a Promise.
44. What is AbortController?
AbortController allows you to cancel certain asynchronous operations, including fetch() requests.
js1const controller = new AbortController(); 2 3fetch("/api/users", { 4 signal: controller.signal 5}); 6 7controller.abort();
This is useful when a request is no longer needed.
Node.js Questions
45. Is Node.js single-threaded?
JavaScript execution in Node.js primarily happens on a single main thread, but Node.js can use additional threads and system facilities for certain operations.
This is why saying simply "Node.js is single-threaded" can be misleading.
📚 Node.js — Don't Block the Event Loop
46. What is the Node.js Event Loop?
The Node.js event loop allows Node.js to handle asynchronous operations without blocking the main JavaScript execution thread.
It is one of the main reasons Node.js works well for I/O-heavy applications.
47. What is process.nextTick()?
process.nextTick() schedules a callback to run after the current operation completes, before the event loop continues to later phases.
js1process.nextTick(() => { 2 console.log("next tick"); 3});
It is specific to Node.js and should be used carefully because excessive use can delay other work.
48. What is CommonJS?
CommonJS is a module system traditionally used by Node.js.
Example:
js1const express = require("express"); 2 3module.exports = router;
It uses require() and module.exports.
49. What are ES Modules?
ES Modules, commonly called ESM, are JavaScript's standard module system.
They use:
js1import express from "express";
and:
js1export default router;
Modern browsers and Node.js support ES Modules.
📚 Node.js — ECMAScript Modules
50. What is the difference between synchronous and asynchronous JavaScript?
Synchronous code runs one operation at a time and waits for each operation to finish.
js1const result = calculate(); 2console.log(result);
Asynchronous code allows JavaScript to start an operation and continue doing other work while waiting for the result.
js1const data = await fetchData();
Understanding asynchronous JavaScript is essential for working with APIs, databases, Node.js, React, and real-world applications.