Top 50 JavaScript Interview Questions and Answers for 2026
Top 50 JavaScript Interview Questions and Answers
Master the most frequently asked JavaScript interview questions covering Closures, Hoisting, Scope, Variables, and Core JavaScript Fundamentals.
JavaScript Fundamentals
1. What is JavaScript?
JavaScript is a high-level, single-threaded, interpreted programming language used to build dynamic and interactive web applications.
2. What are the primitive data types in JavaScript?
- String
- Number
- Boolean
- Undefined
- Null
- Symbol
- BigInt
3. What is the difference between null and undefined?
undefined means a variable has been declared but not assigned a value.
null represents an intentional absence of value.
4. Difference between == and ===?
== performs type coercion before comparison.
=== compares both value and datatype.
"10" == 10 // true "10" === 10 // false
5. What is NaN?
NaN stands for Not a Number and represents an invalid numerical result.
Number("Hello") // NaN
Hoisting Questions
6. What is Hoisting?
Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution.
7. What is hoisted in JavaScript?
- Function Declarations
- var Declarations
- let and const (but not initialized)
8. Output?
console.log(a); var a = 5;
Answer:
undefined
9. Output?
console.log(a); let a = 5;
Answer:
ReferenceError
10. What is Temporal Dead Zone?
The period between variable hoisting and initialization of let or const variables.
11. Are functions hoisted?
Function declarations are fully hoisted.
greet();
function greet() {
console.log("Hello");
}
12. Are function expressions hoisted?
No. Only the variable declaration is hoisted.
sayHi();
var sayHi = function() {
console.log("Hi");
}
Scope Questions
13. What is Scope?
Scope determines where variables can be accessed in a program.
14. Types of Scope?
- Global Scope
- Function Scope
- Block Scope
15. Difference between var, let, and const?
| Feature | var | let | const |
|---|---|---|---|
| Block Scope | No | Yes | Yes |
| Redeclare | Yes | No | No |
| Reassign | Yes | Yes | No |
Closures Questions
16. What is a Closure?
A closure is a function that remembers variables from its outer scope even after the outer function has finished executing.
17. Closure Example
function counter(){
let count = 0;
return function(){
count++;
return count;
}
}
const increment = counter();
console.log(increment());
console.log(increment());
18. Why are Closures useful?
- Private Variables
- Data Hiding
- Memoization
- Event Handlers
- Function Factories
19. What is Lexical Scope?
A child function can access variables from its parent scope.
20. Can Closures cause memory leaks?
Yes. Keeping unnecessary references alive can prevent garbage collection.
Promises Questions
21. What is a Promise in JavaScript?
A Promise is an object that represents the eventual completion or failure of an asynchronous operation.
22. What are the states of a Promise?
- Pending
- Fulfilled (Resolved)
- Rejected
23. Create a Simple Promise
const promise = new Promise(
(resolve, reject) => {
resolve("Success");
}
);
24. What is .then()?
.then() executes when a Promise is resolved successfully.
promise.then((data)=>{
console.log(data);
});
25. What is .catch()?
.catch() handles Promise rejections and errors.
promise.catch((error)=>{
console.log(error);
});
26. What is .finally()?
.finally() runs regardless of whether the Promise succeeds or fails.
27. Difference Between Promise.all() and Promise.allSettled()?
| Method | Behavior |
|---|---|
| Promise.all() | Fails if any Promise fails |
| Promise.allSettled() | Waits for all Promises |
28. What is Promise.race()?
Returns the first settled Promise (resolved or rejected).
29. What is Promise.any()?
Returns the first successfully resolved Promise and ignores rejections.
30. What is Callback Hell?
Callback Hell occurs when multiple nested callbacks make code difficult to read and maintain.
getUser(function(user){
getPosts(user.id,function(posts){
getComments(posts[0].id,function(comments){
console.log(comments);
});
});
});
Async/Await Questions
31. What is async?
The async keyword makes a function automatically return a Promise.
async function hello(){
return "Hello";
}
32. What is await?
await pauses execution until the Promise resolves.
const data = await fetchData();
33. Why use async/await?
- Cleaner Syntax
- Better Readability
- Easier Error Handling
34. How do you handle errors in async/await?
try{
const data =
await fetchData();
}catch(error){
console.log(error);
}
35. Can await be used outside async?
No, except in modern ES modules supporting top-level await.
Event Loop Questions
36. What is the Event Loop?
The Event Loop allows JavaScript to handle asynchronous operations even though JavaScript is single-threaded.
37. Explain Event Loop Architecture
Call Stack ↓ Web APIs ↓ Callback Queue ↓ Event Loop ↓ Call Stack
38. What is the Call Stack?
The Call Stack stores function execution contexts and tracks currently executing functions.
39. What are Web APIs?
Browser-provided features such as:
- setTimeout
- DOM Events
- Fetch API
- Geolocation
40. What is the Callback Queue?
The Callback Queue stores asynchronous callbacks waiting to be executed.
41. Output?
console.log(1);
setTimeout(()=>{
console.log(2);
},0);
console.log(3);
Answer:
1 3 2
42. What is the Microtask Queue?
A high-priority queue used for:
- Promise callbacks
- queueMicrotask()
- MutationObserver
43. Output?
console.log("Start");
Promise.resolve()
.then(()=>{
console.log("Promise");
});
console.log("End");
Answer:
Start End Promise
44. Which executes first: Promise or setTimeout?
Promise callbacks execute first because Microtasks have higher priority than Macrotasks.
this Keyword Questions
45. What is this in JavaScript?
this refers to the object that is executing the current function.
46. What is this in the Global Scope?
Browser:
window
Node.js:
module.exports
47. What is this inside an Object Method?
const user = {
name:"John",
greet(){
console.log(
this.name
);
}
};
this refers to the user object.
48. What is this inside a Regular Function?
Its value depends on how the function is invoked.
49. What is this inside an Arrow Function?
Arrow functions do not have their own this.
They inherit this from the surrounding lexical scope.
50. How can you change the value of this?
Using:
- call()
- apply()
- bind()
function greet(){
console.log(
this.name
);
}
greet.call({
name:"John"
});
Prototype Questions
51. What is a Prototype?
Every JavaScript object has a hidden internal property called a prototype.
Objects inherit properties and methods from their prototype.
const obj = {};
console.log(
obj.__proto__
);
52. What is Prototype Inheritance?
Prototype inheritance allows one object to access properties and methods from another object through the prototype chain.
const animal = {
eat(){
console.log("Eating");
}
};
const dog =
Object.create(animal);
dog.eat();
53. What is the Prototype Chain?
When JavaScript cannot find a property on an object, it searches the object's prototype and continues up the chain until the property is found or null is reached.
54. Difference Between __proto__ and prototype?
| __proto__ | prototype |
|---|---|
| Reference to parent object | Property of constructor function |
| Used by instances | Used for inheritance |
call(), apply(), bind()
55. What is call()?
call() invokes a function immediately and allows you to set the value of this.
function greet(){
console.log(this.name);
}
greet.call({
name:"John"
});
56. What is apply()?
apply() works like call() but accepts arguments as an array.
function sum(a,b){
console.log(a+b);
}
sum.apply(
null,
[10,20]
);
57. What is bind()?
bind() returns a new function with this permanently bound.
const user = {
name:"John"
};
function greet(){
console.log(this.name);
}
const fn =
greet.bind(user);
fn();
58. Difference Between call(), apply(), and bind()?
| Method | Executes Immediately | Arguments |
|---|---|---|
| call() | Yes | Comma Separated |
| apply() | Yes | Array |
| bind() | No | Returns New Function |
Tricky Output Questions
59. Output?
console.log( typeof null );
Answer:
object
This is a historical JavaScript bug.
60. Output?
console.log( NaN === NaN );
Answer:
false
61. Output?
console.log( [] + [] );
Answer:
""
62. Output?
console.log(
[] + {}
);
Answer:
"[object Object]"
63. Output?
console.log( true + true );
Answer:
2
64. Output?
console.log( "5" - 2 );
Answer:
3
65. Output?
console.log( "5" + 2 );
Answer:
"52"
66. Output?
for(var i=0;i<3 i="" settimeout="">{
console.log(i);
},0);
}
3>
Answer:
3 3 3
67. Output?
for(let i=0;i<3 i="" settimeout="">{
console.log(i);
},0);
}
3>
Answer:
0 1 2
Advanced JavaScript Questions
68. What is Event Delegation?
Event Delegation is a technique where a parent element handles events for its child elements using event bubbling.
69. What is Debouncing?
Debouncing delays function execution until a specified time has passed after the last event.
Common use:
- Search Inputs
- API Calls
- Resize Events
70. What is Throttling?
Throttling limits how often a function can execute during a given time period.
71. What is Currying?
function add(a){
return function(b){
return a+b;
}
}
console.log(
add(2)(3)
);
72. What is Memoization?
Memoization stores previously calculated results to improve performance.
73. What is Shallow Copy?
A shallow copy copies only the first level of properties.
const copy = {
...obj
};
74. What is Deep Copy?
A deep copy recursively copies nested objects.
structuredClone(obj);
75. What is Garbage Collection?
JavaScript automatically frees memory that is no longer referenced.
JavaScript Interview Preparation Strategy
Must-Know Topics
- Closures
- Hoisting
- Scope
- Promises
- Async/Await
- Event Loop
- this Keyword
- Prototype Chain
- call(), apply(), bind()
- Debouncing & Throttling
2-Year Experience Interview Checklist
JavaScript Fundamentals ↓ Closures ↓ Hoisting ↓ Promises ↓ Async/Await ↓ Event Loop ↓ DOM Manipulation ↓ API Integration ↓ ES6+ ↓ Interview Ready 🚀
Frequently Asked Questions
Which JavaScript topics are asked most?
Closures, Promises, Hoisting, Event Loop, Async/Await, and this keyword are the most common interview topics.
Are output questions important?
Yes. Many interviewers use output-based questions to test real understanding of JavaScript behavior.
Should I focus on theory or coding?
Both. Strong theoretical knowledge combined with practical coding examples gives the best interview performance.
What level of JavaScript is expected for 2 years experience?
You should be comfortable with asynchronous programming, closures, APIs, debugging, ES6 features, and common design patterns.
Conclusion
JavaScript interviews are less about memorizing syntax and more about understanding how the language works internally.
Mastering Closures, Promises, Hoisting, Event Loop, Async/Await, and the this keyword will help you answer a large percentage of interview questions confidently.
Practice these questions regularly, explain concepts aloud, and connect every concept to real projects you've built.
Strong JavaScript fundamentals remain one of the most valuable skills for frontend, backend, React, and Node.js developers.
Comments
Post a Comment