Syntax:
Output: 10
Benefits:
- Accept unlimited arguments
- Cleaner function handling
Difference Between Spread and Rest:
Rest (...) → Collect values
Spread (...) → Expand values
Spread Example:
Double Tap ❤️ For Part-3
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4));
Output: 10
Benefits:
- Accept unlimited arguments
- Cleaner function handling
Difference Between Spread and Rest:
Rest (...) → Collect values
Spread (...) → Expand values
Spread Example:
const nums = [1, 2, 3];
console.log(...nums);
Double Tap ❤️ For Part-3
❤4
𝗣𝗿𝗼𝗱𝘂𝗰𝘁 𝗠𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁 𝘄𝗶𝘁𝗵 𝗔𝗜 𝗣𝗿𝗼𝗴𝗿𝗮𝗺 by iHUB IIT Roorkee 😍
Freshers get paid 12 LPA average salary for the role of Associate Product Manager! 💼
𝗛𝗶𝗴𝗵𝗹𝗶𝗴𝗵𝘁𝘀:
✅ Learn from IIT Roorkee Professors
✅Placement support from 5,000+ companies
✅ Professional Certification in Product Management with Applied AI
✅ 100% Online Program
✅ Open to Everyone
📅𝗗𝗲𝗮𝗱𝗹𝗶𝗻𝗲: 17th May 2026
𝗔𝗽𝗽𝗹𝘆 𝗡𝗼𝘄👇 :-
https://pdlink.in/4ddJZ5C
⚡ Limited Seats Available — Apply Soon!
Freshers get paid 12 LPA average salary for the role of Associate Product Manager! 💼
𝗛𝗶𝗴𝗵𝗹𝗶𝗴𝗵𝘁𝘀:
✅ Learn from IIT Roorkee Professors
✅Placement support from 5,000+ companies
✅ Professional Certification in Product Management with Applied AI
✅ 100% Online Program
✅ Open to Everyone
📅𝗗𝗲𝗮𝗱𝗹𝗶𝗻𝗲: 17th May 2026
𝗔𝗽𝗽𝗹𝘆 𝗡𝗼𝘄👇 :-
https://pdlink.in/4ddJZ5C
⚡ Limited Seats Available — Apply Soon!
🚀 JavaScript Interview Questions with Answers — Part 3
21. What is an array in JavaScript?
An array is a special object used to store multiple values in a single variable.
Example:
const fruits = ["Apple", "Banana", "Mango"];
Access Elements:
console.log(fruits[0]); // Apple
Features:
• Ordered collection
• Zero-based indexing
• Can store mixed data types
Example:
const data = ["Deepak", 25, true];
22. How do you add/remove elements from an array?
Add Elements
push() → Add at end
const arr = [1, 2];
arr.push(3);
console.log(arr);
unshift() → Add at beginning
arr.unshift(0);
Remove Elements
pop() → Remove from end
arr.pop();
shift() → Remove from beginning
arr.shift();
Example:
const numbers = [1, 2, 3];
numbers.push(4);
numbers.pop();
console.log(numbers);
23. What is the difference between push(), pop(), shift(), unshift()?
push() → Add element at end
pop() → Remove element from end
shift() → Remove element from start
unshift() → Add element at start
Example:
const arr = [1, 2];
arr.push(3);
arr.unshift(0);
console.log(arr);
arr.pop();
arr.shift();
console.log(arr);
Output:
[0][1][2][3]
24. What is map(), filter(), and reduce()?
These are important array methods used in functional programming.
map()
Creates a new array by transforming elements.
const nums = [1, 2, 3];
const doubled = nums.map(num => num * 2);
console.log(doubled);
Output:
[2][4][6]
filter()
Returns elements matching a condition.
const nums = [1, 2, 3, 4];
const even = nums.filter(num => num % 2 === 0);
console.log(even);
Output:
[2][4]
reduce()
Reduces array to a single value.
const nums = [1, 2, 3];
const sum = nums.reduce((total, num) => total + num, 0);
console.log(sum);
Output:
6
25. How do you remove duplicates from an array?
Using Set
const nums = [1, 2, 2, 3, 4, 4];
const unique = [...new Set(nums)];
console.log(unique);
Output:
[1][2][3][4]
Why Set?
A Set stores only unique values.
Alternative Using filter()
const arr = [1, 2, 2, 3];
const unique = arr.filter((item, index) =>
arr.indexOf(item) === index
);
console.log(unique);
26. How do you flat / flatten an array?
Flattening means converting nested arrays into a single array.
Using flat()
const arr = [1, [2, 3], [4, 5]];
console.log(arr.flat());
Output:
[1][2][3][4][5]
Deep Flatten:
const arr = [1, [2, [3, 4]]];
console.log(arr.flat(Infinity));
Using reduce()
const arr = [[1, 2], [3, 4]];
const flat = arr.reduce((acc, val) => acc.concat(val), []);
console.log(flat);
27. What is an object in JavaScript?
An object is a collection of key-value pairs.
Example:
const person = {
name: "Deepak",
age: 25,
city: "Oslo"
};
Access Properties:
console.log(person.name);
Objects Can Store:
• Strings
• Numbers
• Arrays
• Functions
• Other objects
28. What is the difference between dot and bracket notation?
Dot Notation
console.log(person.name);
Bracket Notation
console.log(person["name"]);
Dot Notation
• Simple syntax
• Faster to write
Bracket Notation
• Dynamic keys supported
• Useful for spaces/special chars
Example:
const obj = {
"first name": "Deepak"
};
console.log(obj["first name"]);
29. How do you merge two objects?
Using Spread Operator
const obj1 = {a: 1};
const obj2 = {b: 2};
const merged = {...obj1,...obj2};
console.log(merged);
Output:
{a: 1, b: 2}
Using Object.assign()
const merged = Object.assign({}, obj1, obj2);
Important:
If duplicate keys exist, later values overwrite earlier ones.
30. How do you deep clone an object?
Deep cloning creates a completely independent copy of an object.
Using structuredClone()
const obj = {
name: "Deepak",
address: {
city: "Oslo"
}
};
const clone = structuredClone(obj);
Using JSON Method
const clone = JSON.parse(JSON.stringify(obj));
Double Tap ❤️ For Part-4
21. What is an array in JavaScript?
An array is a special object used to store multiple values in a single variable.
Example:
const fruits = ["Apple", "Banana", "Mango"];
Access Elements:
console.log(fruits[0]); // Apple
Features:
• Ordered collection
• Zero-based indexing
• Can store mixed data types
Example:
const data = ["Deepak", 25, true];
22. How do you add/remove elements from an array?
Add Elements
push() → Add at end
const arr = [1, 2];
arr.push(3);
console.log(arr);
unshift() → Add at beginning
arr.unshift(0);
Remove Elements
pop() → Remove from end
arr.pop();
shift() → Remove from beginning
arr.shift();
Example:
const numbers = [1, 2, 3];
numbers.push(4);
numbers.pop();
console.log(numbers);
23. What is the difference between push(), pop(), shift(), unshift()?
push() → Add element at end
pop() → Remove element from end
shift() → Remove element from start
unshift() → Add element at start
Example:
const arr = [1, 2];
arr.push(3);
arr.unshift(0);
console.log(arr);
arr.pop();
arr.shift();
console.log(arr);
Output:
[0][1][2][3]
24. What is map(), filter(), and reduce()?
These are important array methods used in functional programming.
map()
Creates a new array by transforming elements.
const nums = [1, 2, 3];
const doubled = nums.map(num => num * 2);
console.log(doubled);
Output:
[2][4][6]
filter()
Returns elements matching a condition.
const nums = [1, 2, 3, 4];
const even = nums.filter(num => num % 2 === 0);
console.log(even);
Output:
[2][4]
reduce()
Reduces array to a single value.
const nums = [1, 2, 3];
const sum = nums.reduce((total, num) => total + num, 0);
console.log(sum);
Output:
6
25. How do you remove duplicates from an array?
Using Set
const nums = [1, 2, 2, 3, 4, 4];
const unique = [...new Set(nums)];
console.log(unique);
Output:
[1][2][3][4]
Why Set?
A Set stores only unique values.
Alternative Using filter()
const arr = [1, 2, 2, 3];
const unique = arr.filter((item, index) =>
arr.indexOf(item) === index
);
console.log(unique);
26. How do you flat / flatten an array?
Flattening means converting nested arrays into a single array.
Using flat()
const arr = [1, [2, 3], [4, 5]];
console.log(arr.flat());
Output:
[1][2][3][4][5]
Deep Flatten:
const arr = [1, [2, [3, 4]]];
console.log(arr.flat(Infinity));
Using reduce()
const arr = [[1, 2], [3, 4]];
const flat = arr.reduce((acc, val) => acc.concat(val), []);
console.log(flat);
27. What is an object in JavaScript?
An object is a collection of key-value pairs.
Example:
const person = {
name: "Deepak",
age: 25,
city: "Oslo"
};
Access Properties:
console.log(person.name);
Objects Can Store:
• Strings
• Numbers
• Arrays
• Functions
• Other objects
28. What is the difference between dot and bracket notation?
Dot Notation
console.log(person.name);
Bracket Notation
console.log(person["name"]);
Dot Notation
• Simple syntax
• Faster to write
Bracket Notation
• Dynamic keys supported
• Useful for spaces/special chars
Example:
const obj = {
"first name": "Deepak"
};
console.log(obj["first name"]);
29. How do you merge two objects?
Using Spread Operator
const obj1 = {a: 1};
const obj2 = {b: 2};
const merged = {...obj1,...obj2};
console.log(merged);
Output:
{a: 1, b: 2}
Using Object.assign()
const merged = Object.assign({}, obj1, obj2);
Important:
If duplicate keys exist, later values overwrite earlier ones.
30. How do you deep clone an object?
Deep cloning creates a completely independent copy of an object.
Using structuredClone()
const obj = {
name: "Deepak",
address: {
city: "Oslo"
}
};
const clone = structuredClone(obj);
Using JSON Method
const clone = JSON.parse(JSON.stringify(obj));
Double Tap ❤️ For Part-4
❤7
𝗙𝗥𝗘𝗘 𝗗𝗮𝘁𝗮 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗯𝘆 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 & 𝗟𝗶𝗻𝗸𝗲𝗱𝗜𝗻! 🎓
Stop scrolling! This is your chance to get certified by two of the biggest names in tech— 📊 Level up your Data Skills for FREE!
✅ What you get:
• Official Microsoft & LinkedIn Certification
• High-demand Data Analytics skills
• Perfect for your Resume/LinkedIn profile
𝗘𝗻𝗿𝗼𝗹𝗹 𝗙𝗼𝗿 𝗙𝗥𝗘𝗘👇:-
https://pdlink.in/4ubzzcC
👉Don't miss out on this career upgrade. Limited time offer!
Stop scrolling! This is your chance to get certified by two of the biggest names in tech— 📊 Level up your Data Skills for FREE!
✅ What you get:
• Official Microsoft & LinkedIn Certification
• High-demand Data Analytics skills
• Perfect for your Resume/LinkedIn profile
𝗘𝗻𝗿𝗼𝗹𝗹 𝗙𝗼𝗿 𝗙𝗥𝗘𝗘👇:-
https://pdlink.in/4ubzzcC
👉Don't miss out on this career upgrade. Limited time offer!
❤1
🚀 JavaScript Interview Questions with Answers — Part 4
31. What is if/else and switch?
Both are conditional statements used to make decisions in JavaScript.
if/else
Executes code based on conditions.
switch
Used when checking multiple possible values.
Difference:
if/else - Better for conditions/ranges, Flexible
switch - Better for exact values, Cleaner for many cases
32. What is the difference between for, for...in, and for...of?
for
Traditional loop.
for...in
Used for iterating object keys.
for...of
Used for iterable values like arrays.
Key Difference:
Loop - Best For
for - Full control
for...in - Object properties
for...of - Array values
33. What is the while and do-while loop?
Both loops execute code repeatedly while a condition is true.
while Loop
Condition checked before execution.
do-while Loop
Runs at least once before checking condition.
Difference:
while - Condition first, May run zero times
do-while - Code first, Runs at least once
34. What is the ternary operator?
The ternary operator is a shorthand for if/else.
Syntax:
condition? trueValue : falseValue
Example:
Benefits:
• Shorter code
• Cleaner simple conditions
35. What is short-circuit evaluation?
JavaScript stops evaluating expressions as soon as the result is known.
Using &&
Returns first falsy value.
Output:
false
Using ||
Returns first truthy value.
Output:
Default
Practical Example:
36. What is the difference between break and continue?
Keyword - Purpose
break - Stops the loop completely
continue - Skips current iteration
break Example
Output:
1
2
continue Example
Output:
1
2
4
5
37. How do you iterate over an array or object?
Array Iteration
Using forEach()
Object Iteration
Using for...in
Using Object.keys()
38. How do you implement recursion?
Recursion is when a function calls itself until a stopping condition is met.
Example: Factorial
Output:
120
Important Parts:
1. Base condition
2. Recursive call
Without a base condition → infinite recursion.
31. What is if/else and switch?
Both are conditional statements used to make decisions in JavaScript.
if/else
Executes code based on conditions.
let age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
switch
Used when checking multiple possible values.
let day = 2;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Invalid Day");
}
Difference:
if/else - Better for conditions/ranges, Flexible
switch - Better for exact values, Cleaner for many cases
32. What is the difference between for, for...in, and for...of?
for
Traditional loop.
for (let i = 0; i < 3; i++) {
console.log(i);
}for...in
Used for iterating object keys.
const person = {
name: "Deepak",
age: 25
};
for (let key in person) {
console.log(key);
}for...of
Used for iterable values like arrays.
const nums = [1, 2, 3];
for (let num of nums) {
console.log(num);
}
Key Difference:
Loop - Best For
for - Full control
for...in - Object properties
for...of - Array values
33. What is the while and do-while loop?
Both loops execute code repeatedly while a condition is true.
while Loop
Condition checked before execution.
let i = 1;
while (i <= 3) {
console.log(i);
i++;
}
do-while Loop
Runs at least once before checking condition.
let i = 1;
do {
console.log(i);
i++;
} while(i <= 3);
Difference:
while - Condition first, May run zero times
do-while - Code first, Runs at least once
34. What is the ternary operator?
The ternary operator is a shorthand for if/else.
Syntax:
condition? trueValue : falseValue
Example:
let age = 20;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);
Benefits:
• Shorter code
• Cleaner simple conditions
35. What is short-circuit evaluation?
JavaScript stops evaluating expressions as soon as the result is known.
Using &&
Returns first falsy value.
console.log(false && "Hello");
Output:
false
Using ||
Returns first truthy value.
console.log("" || "Default");Output:
Default
Practical Example:
let username = "";
let displayName = username || "Guest";
console.log(displayName);
36. What is the difference between break and continue?
Keyword - Purpose
break - Stops the loop completely
continue - Skips current iteration
break Example
for (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}Output:
1
2
continue Example
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}Output:
1
2
4
5
37. How do you iterate over an array or object?
Array Iteration
Using forEach()
const nums = [1, 2, 3];
nums.forEach(num => {
console.log(num);
});
Object Iteration
Using for...in
const person = {
name: "Deepak",
age: 25
};
for (let key in person) {
console.log(key, person[key]);
}Using Object.keys()
Object.keys(person).forEach(key => {
console.log(key);
});38. How do you implement recursion?
Recursion is when a function calls itself until a stopping condition is met.
Example: Factorial
function factorial(n) {
if (n === 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5));Output:
120
Important Parts:
1. Base condition
2. Recursive call
Without a base condition → infinite recursion.
❤2
39. When would you use for vs forEach()?
for Loop vs forEach()
for - More control, Can use break/continue, Faster in heavy loops
forEach() - Cleaner syntax, Cannot stop early, Better readability
for Example
forEach() Example
Interview Tip:
Use forEach() for readability and for when more control is needed.
40. How do you handle early exits from loops?
Using break
Using return Inside Functions
Important:
forEach() does not support break directly.
Use:
• for
• for...of
• some()
• every()
for early exits.
Double Tap ❤️ For Part-5
for Loop vs forEach()
for - More control, Can use break/continue, Faster in heavy loops
forEach() - Cleaner syntax, Cannot stop early, Better readability
for Example
for (let i = 0; i < 3; i++) {
console.log(i);
}forEach() Example
[1, 2, 3].forEach(num => {
console.log(num);
});Interview Tip:
Use forEach() for readability and for when more control is needed.
40. How do you handle early exits from loops?
Using break
for (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}Using return Inside Functions
function test() {
for (let i = 1; i <= 5; i++) {
if (i === 3) {
return;
}
console.log(i);
}
}
test();Important:
forEach() does not support break directly.
Use:
• for
• for...of
• some()
• every()
for early exits.
Double Tap ❤️ For Part-5
❤10🍌1
𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗣𝗿𝗼𝗴𝗿𝗮𝗺 𝗧𝗼 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗝𝗼𝗯-𝗥𝗲𝗮𝗱𝘆 𝗦𝗼𝗳𝘁𝘄𝗮𝗿𝗲 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿🔥
No upfront fees. Learn first, pay only after you get placed! 💼✨
🚀 What You’ll Get:
✅ Full Stack Development Training
✅ GenAI + Real Industry Projects
✅ Live Classes & 1:1 Mentorship
✅ Mock Interviews & Resume Support
✅ 500+ Hiring Partners
✅ Average Package: 7.4 LPA
🎯 Ideal for:- Freshers , College Students, Career Switchers & Anyone looking to enter Tech
💻 Learn In-Demand Skills & Build Your Dream Tech Career!
𝐑𝐞𝐠𝐢𝐬𝐭𝐞𝐫 𝐍𝐨𝐰 👇:-
https://pdlink.in/42WOE5H
Hurry! Limited seats are available.🏃♂️
No upfront fees. Learn first, pay only after you get placed! 💼✨
🚀 What You’ll Get:
✅ Full Stack Development Training
✅ GenAI + Real Industry Projects
✅ Live Classes & 1:1 Mentorship
✅ Mock Interviews & Resume Support
✅ 500+ Hiring Partners
✅ Average Package: 7.4 LPA
🎯 Ideal for:- Freshers , College Students, Career Switchers & Anyone looking to enter Tech
💻 Learn In-Demand Skills & Build Your Dream Tech Career!
𝐑𝐞𝐠𝐢𝐬𝐭𝐞𝐫 𝐍𝐨𝐰 👇:-
https://pdlink.in/42WOE5H
Hurry! Limited seats are available.🏃♂️
❤3👍1
📂 Frontend Development
∟📂 Learn HTML
∟📂 Learn CSS
∟📂 Learn JavaScript
∟📂 Learn React
∟📂 Learn Redux
∟📂 Learn TypeScript
📂 Backend Development
∟📂 Learn Node.js
∟📂 Learn Express.js
∟📂 Learn MongoDB
∟📂 RESTful APIs
∟📂 Authentication (JWT, OAuth)
∟📂 GraphQL
∟📂 SQL (e.g., MySQL, PostgreSQL)
∟📂 Database Design
Web Development Best Resources
∟📂 topmate.io/coding/930165
ENJOY LEARNING 👍👍
∟📂 Learn HTML
∟📂 Learn CSS
∟📂 Learn JavaScript
∟📂 Learn React
∟📂 Learn Redux
∟📂 Learn TypeScript
📂 Backend Development
∟📂 Learn Node.js
∟📂 Learn Express.js
∟📂 Learn MongoDB
∟📂 RESTful APIs
∟📂 Authentication (JWT, OAuth)
∟📂 GraphQL
∟📂 SQL (e.g., MySQL, PostgreSQL)
∟📂 Database Design
Web Development Best Resources
∟📂 topmate.io/coding/930165
ENJOY LEARNING 👍👍
❤5
𝗙𝗥𝗘𝗘 𝗢𝗻𝗹𝗶𝗻𝗲 𝗠𝗮𝘀𝘁𝗲𝗿𝗰𝗹𝗮𝘀𝘀 𝗢𝗻 𝗗𝗮𝘁𝗮 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀 ( 𝗕𝘂𝘀𝗶𝗻𝗲𝘀𝘀 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀)😍
Learn the Latest 5 Analytics Tools in 2026
Learn Essential skills to stay competitive in the evolving job market
Eligibility :- Students ,Graduates & Working Professionals
𝗥𝗲𝗴𝗶𝘀𝘁𝗲𝗿 𝗙𝗼𝗿 𝗙𝗥𝗘𝗘 👇:-
https://pdlink.in/4tFlovr
(Limited Slots ..HurryUp🏃♂️ )
𝐃𝐚𝐭𝐞 & 𝐓𝐢𝐦𝐞:- 20th May 2026, at 7 PM
Learn the Latest 5 Analytics Tools in 2026
Learn Essential skills to stay competitive in the evolving job market
Eligibility :- Students ,Graduates & Working Professionals
𝗥𝗲𝗴𝗶𝘀𝘁𝗲𝗿 𝗙𝗼𝗿 𝗙𝗥𝗘𝗘 👇:-
https://pdlink.in/4tFlovr
(Limited Slots ..HurryUp🏃♂️ )
𝐃𝐚𝐭𝐞 & 𝐓𝐢𝐦𝐞:- 20th May 2026, at 7 PM