Top JavaScript Interview Questions and Answers for Freshers | Crack 3 to 10 LPA Jobs
🚀Basic JavaScript Questions & Answers
1. What are the key differences between var
, let
, and const
?
var
: Function-scoped, can be redeclared.let
: Block-scoped, cannot be redeclared.const
: Block-scoped, cannot be reassigned.
2. Explain primitive and reference types in JavaScript.
- Primitive types:
string
,number
,boolean
,null
,undefined
,symbol
,bigint
. - Reference types: Objects, arrays, functions (stored in heap memory).
3. What is the difference between ==
and ===
?
==
(loose equality): Converts types before comparison.===
(strict equality): Compares both value and type.
4. What is a closure in JavaScript? Give an example.
A closure is a function that remembers variables from its outer scope even after execution.
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
5. Explain the concept of "hoisting" in JavaScript.
- Hoisting moves function and variable declarations to the top before execution.
- Variables declared with
var
are hoisted but not initialized. let
andconst
are hoisted but not accessible before declaration.
6. What is the this
keyword, and how does it work?
this
refers to the object that is calling the function.- In arrow functions,
this
is lexically scoped (inherited from the surrounding scope).
7. What are arrow functions, and how are they different from regular functions?
- No
this
binding (inherits from the surrounding scope). - Cannot use
arguments
object. - Cannot be used as constructors.
8. What are template literals in ES6?
- Allows multi-line strings and embedded expressions using backticks (`).
let name = "Byomkesh";
console.log(`Hello, ${name}!`);
9. What is the spread (...
) operator? Give an example.
- Expands iterable elements.
let arr = [1, 2, 3];
let newArr = [...arr, 4, 5]; // [1,2,3,4,5]
10. What is destructuring in JavaScript?
- Extracts values from arrays/objects into variables.
let [a, b] = [10, 20];
let { name, age } = { name: "John", age: 25 };
Intermediate JavaScript Questions & Answers
11. What is an Immediately Invoked Function Expression (IIFE)?
- A function that runs immediately after defining it.
(function() { console.log("IIFE"); })();
12. What are promises in JavaScript?
- Used to handle asynchronous operations.
fetch("api.com").then(res => res.json()).catch(err => console.error(err));
13. What is async/await
?
- A modern way to handle promises.
async function fetchData() {
let data = await fetch("api.com");
}
14. What is event delegation?
- Instead of adding listeners to multiple child elements, attach one listener to a parent element.
15. What is the difference between call, apply, and bind?
call()
: Calls function withthis
and arguments.apply()
: Similar but takes an array of arguments.bind()
: Returns a new function withthis
bound.
16. What is debounce
and throttle
function?
debounce()
: Executes after a delay.throttle()
: Executes at a fixed interval.
17. What is the difference between deep copy and shallow copy in JavaScript?
- Shallow Copy: Copies only references (
Object.assign()
, spread operator). - Deep Copy: Creates an independent copy (
JSON.parse(JSON.stringify(obj))
).
18. Explain how JavaScript handles memory and garbage collection.
- Uses Garbage Collection (GC) to remove unused variables and objects.
19. What are higher-order functions? Give an example.
- Functions that take other functions as arguments or return functions.
function add(a) {
return function (b) { return a + b; };
}
20. What are the different ways to loop through an array in JavaScript?
for
,forEach()
,map()
,filter()
,reduce()
.
Advanced JavaScript Questions & Answers
21. Explain the event loop in JavaScript.
- Manages asynchronous code execution using Call Stack, Task Queue, and Microtasks.
22. What is the difference between map()
, filter()
, and reduce()
?
map()
: Transforms an array.filter()
: Filters elements based on condition.reduce()
: Reduces array to a single value.
23. What is a WeakMap and WeakSet?
- Similar to
Map
andSet
, but keys are weakly held (prevents memory leaks).
24. What are JavaScript generators, and how do they work?
- Functions that can be paused and resumed.
function* genFunc() { yield 1; yield 2; }
let it = genFunc();
console.log(it.next()); // {value: 1, done: false}
25. How does the setTimeout()
and setInterval()
function work?
setTimeout()
: Executes once after a delay.setInterval()
: Executes repeatedly at intervals.
React.js (If Applicable for Frontend Roles)
41. What is React, and why is it used?
- A JavaScript library for building user interfaces with a component-based structure.
42. What is JSX? How is it different from HTML?
- JSX is a syntax extension of JavaScript that allows writing HTML-like code inside JS.
43. Explain React state and props.
- State: Manages component data internally.
- Props: Passes data from parent to child component.
Node.js (If Applicable for Backend Roles)
51. What is Node.js, and how does it work?
- A JavaScript runtime built on Chrome’s V8 engine for backend development.
52. What is Express.js, and why is it used?
- A web framework for Node.js that simplifies server-side development.
53. What is middleware in Express.js?
- Functions that modify request/response objects before reaching route handlers.
Database & Other Backend Concepts
61. What is MongoDB? How does it differ from SQL databases?
- NoSQL database storing data as JSON-like documents (vs. tables in SQL).
62. What is Mongoose? How does it help in working with MongoDB?
- An ODM (Object Data Modeling) library for MongoDB.
This should give you a quick revision of all essential JavaScript concepts for fresher interviews (3-10 LPA range). 🚀 Let me know if you want detailed explanations or code examples for any! 🎯