๐๐ ๐ฎ๐ป๐ฑ ๐ ๐ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ ๐ฏ๐ ๐๐๐, ๐๐๐ง ๐ ๐ฎ๐ป๐ฑ๐ถ๐
Freshers get 15 LPA Average Salary with AI & ML Skills!
๐ป 100% Online
โณ 6 Months Duration
๐จโ๐ซ Learn from IIT Professors
๐ Open for Students ,Freshers & Working Professionals
๐ผ Placement Assistance with 5000+ Companies
๐ High Demand Skills for Future Tech Jobs
Top companies are hiring for candidates with ๐๐, ๐ ๐ฎ๐ฐ๐ต๐ถ๐ป๐ฒ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด skills in 2026
๐ฅDeadline :- 17th May
๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐ :-
https://pdlink.in/4nmI024
.
Get Placement Assistance With 5000+ Companies
Freshers get 15 LPA Average Salary with AI & ML Skills!
๐ป 100% Online
โณ 6 Months Duration
๐จโ๐ซ Learn from IIT Professors
๐ Open for Students ,Freshers & Working Professionals
๐ผ Placement Assistance with 5000+ Companies
๐ High Demand Skills for Future Tech Jobs
Top companies are hiring for candidates with ๐๐, ๐ ๐ฎ๐ฐ๐ต๐ถ๐ป๐ฒ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด skills in 2026
๐ฅDeadline :- 17th May
๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐ :-
https://pdlink.in/4nmI024
.
Get Placement Assistance With 5000+ Companies
โค3
๐ JavaScript Interview Questions with Answers โ Part 1
๐ง 1. What is JavaScript and what is it used for?
JavaScript is a high-level, interpreted programming language mainly used to make websites interactive and dynamic. It runs inside the browser and can also run on servers using Node.js.
Common Uses of JavaScript:
โข Building interactive websites
โข Form validation
โข Animations and sliders
โข API calls and dynamic content
โข Web apps and mobile apps
โข Backend development with Node.js
โข Game development
Example: console.log("Hello World");
2. What are the data types in JavaScript?
JavaScript has two categories of data types:
Primitive Data Types
1. String
2. Number
3. Boolean
4. Undefined
5. Null
6. BigInt
7. Symbol
Reference Data Types
1. Object
2. Array
3. Function
Example:
let name = "Deepak"; // String
let age = 25; // Number
let isActive = true; // Boolean
let data = null; // Null
let value; // Undefined
3. What is the difference between null and undefined?
Feature | null | undefined
Meaning | Intentional empty value | Variable not assigned
Type | object | undefined
Assigned by | Developer | JavaScript automatically
Example:
let a = null;
let b;
console.log(a); // null
console.log(b); // undefined
Key Point:
โข null means โempty intentionallyโ
โข undefined means โvalue not assigned yetโ
4. What is the difference between == and ===?
== (Loose Equality)
โข Compares values only
โข Performs type conversion (type coercion)
=== (Strict Equality)
โข Compares both value and data type
โข No type conversion
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false
Interview Tip: Always prefer === because it gives more predictable results.
5. What are primitive vs reference types?
Primitive Types
Stored directly in memory.
Examples: String, Number, Boolean, Null, Undefined
Reference Types
Stored by reference (memory address).
Examples: Objects, Arrays, Functions
Example:
let a = 10;
let b = a;
b = 20;
console.log(a); // 10
Reference Example:
let obj1 = {name: "John"};
let obj2 = obj1;
obj2.name = "Mike";
console.log(obj1.name); // Mike
Key Difference:
โข Primitive โ copied by value
โข Reference โ copied by reference
6. What is type coercion?
Type coercion means JavaScript automatically converts one data type into another during operations or comparisons.
Example:
console.log("5" + 2); // "52"
console.log("5" - 2); // 3
Why?
โข + prefers string concatenation
โข - converts strings to numbers
Types of Coercion:
1. Implicit coercion (automatic)
2. Explicit coercion (manual)
Explicit Example: Number("10"); // 10 , String(123); // "123"
7. What is the difference between let, const, and var?
Feature | var | let | const
Scope | Function | Block | Block
Reassign | Yes | Yes | No
Redeclare | Yes | No | No
Hoisted | Yes | Yes | Yes
Example:
var a = 10;
let b = 20;
const c = 30;
Key Points:
โข Use let for changing values
โข Use const for fixed values
โข Avoid var in modern JavaScript
8. What is block-scope vs function-scope?
Function Scope
Accessible inside the entire function. var is function-scoped.
Block Scope
Accessible only inside {} block. let and const are block-scoped.
Example:
function test() {
if (true) {
var a = 10;
let b = 20;
}
console.log(a); // Works
console.log(b); // Error
}
9. What is the difference between let and var?
Feature | var | let
Scope | Function | Block
Redeclare | Allowed | Not allowed
Hoisting | Yes | Yes (TDZ applies)
Example:
var x = 10;
var x = 20; // Allowed
let y = 10;
let y = 20; // Error
Important: let avoids many bugs caused by var.
10. How do you declare and use variables?
Variables are used to store data.
Syntax:
let name = "Deepak";
const age = 25;
Rules:
- Use meaningful names
- Cannot start with numbers
- Case-sensitive
Double Tap โค๏ธ For Part-2
๐ง 1. What is JavaScript and what is it used for?
JavaScript is a high-level, interpreted programming language mainly used to make websites interactive and dynamic. It runs inside the browser and can also run on servers using Node.js.
Common Uses of JavaScript:
โข Building interactive websites
โข Form validation
โข Animations and sliders
โข API calls and dynamic content
โข Web apps and mobile apps
โข Backend development with Node.js
โข Game development
Example: console.log("Hello World");
2. What are the data types in JavaScript?
JavaScript has two categories of data types:
Primitive Data Types
1. String
2. Number
3. Boolean
4. Undefined
5. Null
6. BigInt
7. Symbol
Reference Data Types
1. Object
2. Array
3. Function
Example:
let name = "Deepak"; // String
let age = 25; // Number
let isActive = true; // Boolean
let data = null; // Null
let value; // Undefined
3. What is the difference between null and undefined?
Feature | null | undefined
Meaning | Intentional empty value | Variable not assigned
Type | object | undefined
Assigned by | Developer | JavaScript automatically
Example:
let a = null;
let b;
console.log(a); // null
console.log(b); // undefined
Key Point:
โข null means โempty intentionallyโ
โข undefined means โvalue not assigned yetโ
4. What is the difference between == and ===?
== (Loose Equality)
โข Compares values only
โข Performs type conversion (type coercion)
=== (Strict Equality)
โข Compares both value and data type
โข No type conversion
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false
Interview Tip: Always prefer === because it gives more predictable results.
5. What are primitive vs reference types?
Primitive Types
Stored directly in memory.
Examples: String, Number, Boolean, Null, Undefined
Reference Types
Stored by reference (memory address).
Examples: Objects, Arrays, Functions
Example:
let a = 10;
let b = a;
b = 20;
console.log(a); // 10
Reference Example:
let obj1 = {name: "John"};
let obj2 = obj1;
obj2.name = "Mike";
console.log(obj1.name); // Mike
Key Difference:
โข Primitive โ copied by value
โข Reference โ copied by reference
6. What is type coercion?
Type coercion means JavaScript automatically converts one data type into another during operations or comparisons.
Example:
console.log("5" + 2); // "52"
console.log("5" - 2); // 3
Why?
โข + prefers string concatenation
โข - converts strings to numbers
Types of Coercion:
1. Implicit coercion (automatic)
2. Explicit coercion (manual)
Explicit Example: Number("10"); // 10 , String(123); // "123"
7. What is the difference between let, const, and var?
Feature | var | let | const
Scope | Function | Block | Block
Reassign | Yes | Yes | No
Redeclare | Yes | No | No
Hoisted | Yes | Yes | Yes
Example:
var a = 10;
let b = 20;
const c = 30;
Key Points:
โข Use let for changing values
โข Use const for fixed values
โข Avoid var in modern JavaScript
8. What is block-scope vs function-scope?
Function Scope
Accessible inside the entire function. var is function-scoped.
Block Scope
Accessible only inside {} block. let and const are block-scoped.
Example:
function test() {
if (true) {
var a = 10;
let b = 20;
}
console.log(a); // Works
console.log(b); // Error
}
9. What is the difference between let and var?
Feature | var | let
Scope | Function | Block
Redeclare | Allowed | Not allowed
Hoisting | Yes | Yes (TDZ applies)
Example:
var x = 10;
var x = 20; // Allowed
let y = 10;
let y = 20; // Error
Important: let avoids many bugs caused by var.
10. How do you declare and use variables?
Variables are used to store data.
Syntax:
let name = "Deepak";
const age = 25;
Rules:
- Use meaningful names
- Cannot start with numbers
- Case-sensitive
Double Tap โค๏ธ For Part-2
โค6๐1๐คฃ1๐ญ1
๐ ๐๐ฒ๐ฐ๐ผ๐บ๐ฒ ๐๐ผ๐ฏ-๐ฅ๐ฒ๐ฎ๐ฑ๐ ๐ถ๐ป ๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ & ๐๐ ๐๐ถ๐๐ต ๐๐ป๐ฑ๐๐๐๐ฟ๐ ๐๐
๐ฝ๐ฒ๐ฟ๐๐! ๐
Learn the most in-demand skills of 2026
๐ซData Science ,AI,ML &Python & SQL
โ
๐ผ Get Placement Assistance
๐ Beginner Friendly Program
๐ป Learn Online from Anywhere
๐ Build Skills Companies Actually Hire For
๐ฅ AI is changing every industry โ this is the best time to upskill and secure high-paying tech jobs.
๐๐๐ ๐ข๐ฌ๐ญ๐๐ซ ๐๐จ๐ฐ ๐:-
https://pdlink.in/4fdWxJB
โก Limited Seats Available โ Apply Fast!
Learn the most in-demand skills of 2026
๐ซData Science ,AI,ML &Python & SQL
โ
๐ผ Get Placement Assistance
๐ Beginner Friendly Program
๐ป Learn Online from Anywhere
๐ Build Skills Companies Actually Hire For
๐ฅ AI is changing every industry โ this is the best time to upskill and secure high-paying tech jobs.
๐๐๐ ๐ข๐ฌ๐ญ๐๐ซ ๐๐จ๐ฐ ๐:-
https://pdlink.in/4fdWxJB
โก Limited Seats Available โ Apply Fast!
โค3
๐ JavaScript Interview Questions with Answers โ Part 2
11. What is a function in JavaScript?
A function is a reusable block of code designed to perform a specific task.
Why Functions Are Important:
โข Reuse code
โข Improve readability
โข Reduce duplication
โข Make programs modular
Syntax:
Calling a Function: greet();
Function With Parameters:
12. What is a function declaration vs expression?
Function Declaration
Defined using the function keyword with a name.
Function Expression
Function stored inside a variable.
Feature Comparison:
Hoisted โ Declaration: Yes, Expression: No
Named โ Declaration: Usually, Expression: Can be anonymous
Key Point:
Function declarations can be called before they are defined because of hoisting.
13. What is an arrow function?
Arrow functions are a shorter syntax for writing functions introduced in ES6.
Syntax:
Example With Parameters:
Benefits:
โข Shorter syntax
โข Cleaner code
โข No own this binding
Important:
Arrow functions should not be used as object methods when this is required.
14. What is hoisting?
Hoisting is JavaScriptโs behavior of moving declarations to the top of the scope before execution.
Example:
Internally:
Output: undefined
Important Points:
โข var is hoisted and initialized as undefined
โข let and const are hoisted but stay in the Temporal Dead Zone (TDZ)
Function Hoisting:
15. What is a closure?
A closure is created when an inner function remembers variables from its outer function even after the outer function has finished execution.
Example:
Why Closures Are Useful:
โข Data privacy
โข Maintaining state
โข Callbacks
โข Memoization
Interview Definition:
A closure gives a function access to its outer scope even after the outer function is executed.
16. What is the module pattern?
The module pattern is used to create private and public variables/functions using closures.
Example:
Counter.increment();
Counter.increment();
Benefits:
โข Encapsulation
โข Data hiding
โข Avoids global scope pollution
17. What is IIFE?
IIFE stands for:
Immediately Invoked Function Expression
It runs immediately after being created.
Syntax:
Arrow Function IIFE:
Why Use IIFE?
โข Avoid global variables
โข Create private scope
โข Execute code instantly
18. What is the difference between function parameters and arguments?
Parameters โ Variables in function definition
Arguments โ Actual values passed to function
Example:
Key Point:
โข Parameters receive values
โข Arguments send values
19. What is a default parameter?
Default parameters allow functions to use a default value if no argument is passed.
Example:
Output:
Hello Guest
Hello Deepak
Benefit:
Prevents undefined values.
20. How do optional / rest parameters (...args) work?
Rest parameters collect multiple arguments into a single array.
11. What is a function in JavaScript?
A function is a reusable block of code designed to perform a specific task.
Why Functions Are Important:
โข Reuse code
โข Improve readability
โข Reduce duplication
โข Make programs modular
Syntax:
function greet() {
console.log("Hello");
}
Calling a Function: greet();
Function With Parameters:
function greet(name) {
console.log("Hello " + name);
}
greet("Deepak");
12. What is a function declaration vs expression?
Function Declaration
Defined using the function keyword with a name.
function add(a, b) {
return a + b;
}
Function Expression
Function stored inside a variable.
const add = function(a, b) {
return a + b;
};
Feature Comparison:
Hoisted โ Declaration: Yes, Expression: No
Named โ Declaration: Usually, Expression: Can be anonymous
Key Point:
Function declarations can be called before they are defined because of hoisting.
13. What is an arrow function?
Arrow functions are a shorter syntax for writing functions introduced in ES6.
Syntax:
const greet = () => {
console.log("Hello");
};
Example With Parameters:
const add = (a, b) => a + b;
console.log(add(2, 3));
Benefits:
โข Shorter syntax
โข Cleaner code
โข No own this binding
Important:
Arrow functions should not be used as object methods when this is required.
14. What is hoisting?
Hoisting is JavaScriptโs behavior of moving declarations to the top of the scope before execution.
Example:
console.log(a);
var a = 10;
Internally:
var a;
console.log(a);
a = 10;
Output: undefined
Important Points:
โข var is hoisted and initialized as undefined
โข let and const are hoisted but stay in the Temporal Dead Zone (TDZ)
Function Hoisting:
sayHello();
function sayHello() {
console.log("Hello");
}
15. What is a closure?
A closure is created when an inner function remembers variables from its outer function even after the outer function has finished execution.
Example:
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
Why Closures Are Useful:
โข Data privacy
โข Maintaining state
โข Callbacks
โข Memoization
Interview Definition:
A closure gives a function access to its outer scope even after the outer function is executed.
16. What is the module pattern?
The module pattern is used to create private and public variables/functions using closures.
Example:
const Counter = (function() {
let count = 0;
return {
increment: function() {
count++;
console.log(count);
},
decrement: function() {
count--;
console.log(count);
}
};
})();
Counter.increment();
Counter.increment();
Benefits:
โข Encapsulation
โข Data hiding
โข Avoids global scope pollution
17. What is IIFE?
IIFE stands for:
Immediately Invoked Function Expression
It runs immediately after being created.
Syntax:
(function() {
console.log("IIFE Executed");
})();
Arrow Function IIFE:
(() => {
console.log("Hello");
})();
Why Use IIFE?
โข Avoid global variables
โข Create private scope
โข Execute code instantly
18. What is the difference between function parameters and arguments?
Parameters โ Variables in function definition
Arguments โ Actual values passed to function
Example:
function greet(name) { // Parameter
console.log(name);
}
greet("Deepak"); // Argument
Key Point:
โข Parameters receive values
โข Arguments send values
19. What is a default parameter?
Default parameters allow functions to use a default value if no argument is passed.
Example:
function greet(name = "Guest") {
console.log("Hello " + name);
}
greet();
greet("Deepak");
Output:
Hello Guest
Hello Deepak
Benefit:
Prevents undefined values.
20. How do optional / rest parameters (...args) work?
Rest parameters collect multiple arguments into a single array.
โค4
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