JavaScript Interview Guide 2025
JavaScript Interview Guide 2025
July 2025
Preface 7
1 Fundamentals of JavaScript 9
1.1 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2 Scopes and Lexical Scope . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4 Functions and Arrow Functions . . . . . . . . . . . . . . . . . . . . . . 11
1.4.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Asynchronous JavaScript 15
2.1 Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Async/Await . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2
CONTENTS 3
3.8.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.8.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.9 Destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.9.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.9.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.9.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.9.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.10 Spread and Rest Operators . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.10.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.10.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.10.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.10.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.11 Map and Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.11.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.11.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.11.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.11.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.12 Template Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.12.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.12.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.12.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.12.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.13 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.13.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.13.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.13.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.13.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.14 Prototypal Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.14.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.14.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.14.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.14.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.15 Strict Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.15.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.15.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.15.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.15.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.16 Debouncing and Throttling . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.16.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.16.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.16.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.16.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.17 Web Workers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.17.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.17.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.17.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.17.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.18 Memory Management and Garbage Collection . . . . . . . . . . . . . 29
CONTENTS 5
3.18.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.18.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.18.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.18.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.19 Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.19.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.19.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.19.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.19.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.20 Equality Operators (== vs ===) . . . . . . . . . . . . . . . . . . . . . . . 30
3.20.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.20.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.20.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.20.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.21 IIFE (Immediately Invoked Function Expression) . . . . . . . . . . . 31
3.21.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.21.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.21.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.21.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.22 Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.22.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.22.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.22.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.22.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.23 Higher-Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.23.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.23.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.23.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.23.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.24 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.24.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.24.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.24.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.24.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.25 Event Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.25.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.25.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.25.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.25.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.26 Polyfills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.26.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.26.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.26.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.26.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.27 Type Coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.27.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.27.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.27.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6 CONTENTS
4 Conclusion 35
Preface
7
8 CONTENTS
1 Fundamentals of JavaScript
1.1.1 Question
What are the different ways to declare variables in JavaScript, and how do they
differ?
1.1.2 Explanation
JavaScript offers three variable declarations: var, let, and const. var is function-
scoped, hoisted with undefined, and allows redeclaration and reassignment.
let is block-scoped, hoisted but not initialized, and allows reassignment but not
redeclaration. const is block-scoped, cannot be reassigned or redeclared, but
object properties can be modified. JavaScript supports data types like numbers,
strings, booleans, objects, arrays, null, undefined, and symbols (ES6). Under-
standing these differences is critical for managing scope and avoiding bugs in
interviews.
1.1.3 Example
1 // Using var: function-scoped, hoisted
2 var x = 10; // Can redeclare
3 var x = 20; // No error
4 x = 30; // Reassignable
5 [Link](x); // Output: 30
6
9
10 CHAPTER 1. FUNDAMENTALS OF JAVASCRIPT
1.2.1 Question
What is scope in JavaScript, and how does lexical scope work?
1.2.2 Explanation
Scope defines variable accessibility. JavaScript has global, function, and block
scopes. Lexical scope means a function’s scope is determined by its definition
location, not execution. Inner functions access outer scope variables, enabling
closures. This is a common interview topic to test understanding of variable
access and scope chains.
1.2.3 Example
1 function outer() {
2 let outerVar = ’I\’m outside’;
3
4 function inner() {
5 [Link](outerVar); // Accesses outerVar due to lexical scope
6 }
7 inner();
8 }
9 outer(); // Output: I’m outside
10
1.3 Closures
1.3.1 Question
What is a closure, and how is it used in JavaScript?
1.4. FUNCTIONS AND ARROW FUNCTIONS 11
1.3.2 Explanation
A closure is a function that retains access to its lexical scope after execution.
Closures are used for data privacy, function factories, and state persistence. They
are common in interviews for testing scope and encapsulation knowledge.
1.3.3 Example
1 function createCounter() {
2 let count = 0; // Private variable
3 return function() {
4 count++;
5 return count;
6 };
7 }
8 const counter = createCounter();
9 [Link](counter()); // Output: 1
10 [Link](counter()); // Output: 2
11
1.4.2 Explanation
Regular functions have their own this context, arguments object, and can be
constructors. Arrow functions inherit this from the surrounding scope, lack
arguments, and are concise but cannot be constructors. This distinction is fre-
quently tested in interviews.
1.4.3 Example
1 function regularFunction() {
2 [Link](this); // Refers to calling object
3 }
12 CHAPTER 1. FUNDAMENTALS OF JAVASCRIPT
9 const obj = {
10 name: ’Test’,
11 regular: regularFunction,
12 arrow: arrowFunction
13 };
14 [Link](); // Output: { name: ’Test’, ... }
15 [Link](); // Output: window (or surrounding this)
16
1.5.2 Explanation
Conditional statements (if, else if, else, switch) control program flow based
on conditions. They are used for decision-making, such as validating inputs or
rendering UI elements. Interviewers may ask for optimized conditionals or edge
cases.
1.5.3 Example
1 let age = 20;
2 if (age < 18) {
3 [Link](’Minor’);
4 } else if (age >= 18 && age < 65) {
5 [Link](’Adult’);
6 } else {
7 [Link](’Senior’);
8 }
9 // Output: Adult
10
11 let day = 2;
12 switch (day) {
13 case 1:
14 [Link](’Monday’);
1.5. CONDITIONAL STATEMENTS 13
15 break;
16 case 2:
17 [Link](’Tuesday’);
18 break;
19 default:
20 [Link](’Invalid day’);
21 }
22 // Output: Tuesday
2.1 Promises
2.1.1 Question
What are Promises, and how do you handle asynchronous operations with them?
2.1.2 Explanation
A Promise represents a future value with states: pending, fulfilled, or rejected.
It uses .then() for success and .catch() for errors. Promises handle asyn-
chronous tasks like API calls, a common interview focus.
2.1.3 Example
1 const fetchData = new Promise((resolve, reject) => {
2 setTimeout(() => {
3 const success = true;
4 if (success) {
5 resolve(’Data fetched!’);
6 } else {
7 reject(’Error fetching data’);
8 }
9 }, 1000);
10 });
11
12 fetchData
13 .then(result => [Link](result)) // Output: Data fetched!
14 .catch(error => [Link](error));
15
16 // Chaining example
17 fetchData
18 .then(result => [Link]())
19 .then(upper => [Link](upper)) // Output: DATA FETCHED!
20 .catch(error => [Link](error));
15
16 CHAPTER 2. ASYNCHRONOUS JAVASCRIPT
2.2 Async/Await
2.2.1 Question
How does async/await simplify working with Promises?
2.2.2 Explanation
async/await makes asynchronous code synchronous-like. An async function
returns a Promise, and await pauses execution until the Promise resolves. It’s
cleaner than .then() chains and often tested in interviews.
2.2.3 Example
1 async function getData() {
2 try {
3 const response = await new Promise(resolve => {
4 setTimeout(() => resolve(’Data received!’), 1000);
5 });
6 [Link](response); // Output: Data received!
7 return [Link]();
8 } catch (error) {
9 [Link](error);
10 }
11 }
12 getData().then(result => [Link](result)); // Output: DATA
RECEIVED!
2.3 Callbacks
2.3.1 Question
What is a callback function, and how is it used?
2.3.2 Explanation
A callback is a function passed as an argument, executed after an operation com-
pletes, often in asynchronous tasks. Callbacks can lead to callback hell, making
Promises or async/await preferable.
2.3.3 Example
1 function fetchUser(callback) {
2 setTimeout(() => {
3 callback({ name: ’John’, age: 30 });
4 }, 1000);
2.3. CALLBACKS 17
5 }
6
7 fetchUser(user => {
8 [Link](user); // Output: { name: ’John’, age: 30 }
9 });
10
3.1.1 Question
How do you create and manipulate objects, and what is prototypal inheritance?
3.1.2 Explanation
3.1.3 Example
1 const person = {
2 name: ’Alice’,
3 greet() {
4 [Link](‘Hello, ${[Link]}!‘);
5 }
6 };
7 [Link](); // Output: Hello, Alice!
8
9 function Animal(type) {
10 [Link] = type;
11 }
12 [Link] = function() {
13 [Link](‘${[Link]} makes a sound‘);
14 };
15 const dog = new Animal(’Dog’);
16 [Link](); // Output: Dog makes a sound
19
20 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS
dog
[[Prototype]]
[Link]
[[Prototype]]
[Link]
3.2.2 Explanation
ES6 array methods like map, filter, reduce, forEach, find, and some enable
functional programming. They are frequently tested for data manipulation tasks.
3.2.3 Example
1 const numbers = [1, 2, 3, 4];
2
3.3.2 Explanation
The event loop manages JavaScript’s single-threaded, asynchronous nature by
processing the call stack, task queue, and microtask queue. Microtasks (e.g.,
Promises) run before tasks (e.g., setTimeout). This is a common interview ques-
tion to test asynchronous understanding.
3.3.3 Example
1 [Link](’Start’);
2 setTimeout(() => [Link](’Timeout’), 0);
3 [Link]().then(() => [Link](’Promise’));
4 [Link](’End’);
5 // Output: Start, End, Promise, Timeout
3.4.2 Explanation
ES6+ introduced arrow functions, destructuring, spread/rest operators, template
literals, default parameters, modules, optional chaining (?.), and nullish coalesc-
ing (??). These enhance code readability and functionality.
22 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS
3.4.3 Example
1 const { name, age } = { name: ’Bob’, age: 25 };
2 [Link](name, age); // Output: Bob 25
3
3.5.2 Explanation
Arrays simulate stacks (LIFO) with push and pop, or queues (FIFO) with push
and shift. These are tested for understanding basic data structures.
3.5.3 Example
1 const stack = [];
2 [Link](1); // Add to top
3 [Link](2);
4 [Link]([Link]()); // Output: 2 (LIFO)
5
3.6.2 Explanation
Errors are handled with try/catch for synchronous code and .catch() or
try/catch with async/await for asynchronous code. Custom errors can be
thrown with throw.
3.6.3 Example
1 try {
2 throw new Error(’Something went wrong’);
3 } catch (error) {
4 [Link]([Link]); // Output: Something went wrong
5 }
6
3.7.2 Explanation
ES6 modules use export and import for modularity, supporting named and de-
fault exports. They are static and resolved at compile time.
3.7.3 Example
1 // [Link]
2 export const add = (a, b) => a + b;
3 export const subtract = (a, b) => a - b;
4
5 // [Link]
24 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS
3.8 Hoisting
3.8.1 Question
What is hoisting in JavaScript?
3.8.2 Explanation
Hoisting moves variable and function declarations to the top of their scope. var
is hoisted with undefined; let and const are hoisted but not initialized (TDZ).
3.8.3 Example
1 [Link](x); // Output: undefined
2 var x = 10;
3
7 function hoisted() {
8 [Link](’Hoisted!’);
9 }
10 hoisted(); // Output: Hoisted!
3.9 Destructuring
3.9.1 Question
What is destructuring in ES6, and how is it used?
3.9.2 Explanation
Destructuring extracts values from arrays or objects into variables, improving
code readability. It’s used for props, function parameters, and swaps.
3.10. SPREAD AND REST OPERATORS 25
3.9.3 Example
1 const [a, b] = [1, 2];
2 [Link](a, b); // Output: 1 2
3
3.10.1 Question
What are the spread and rest operators in ES6?
3.10.2 Explanation
The spread operator (...) expands iterables; the rest operator collects argu-
ments into an array. Both enhance flexibility in arrays and objects.
3.10.3 Example
1 const arr1 = [1, 2];
2 const arr2 = [...arr1, 3, 4];
3 [Link](arr2); // Output: [1, 2, 3, 4]
4
5 const obj1 = { a: 1, b: 2 };
6 const obj2 = { ...obj1, c: 3 };
7 [Link](obj2); // Output: { a: 1, b: 2, c: 3 }
8
9 function sum(...numbers) {
10 return [Link]((total, num) => total + num, 0);
11 }
12 [Link](sum(1, 2, 3, 4)); // Output: 10
3.11.2 Explanation
Map stores key-value pairs with any key type; Set stores unique values. Both
provide better performance for certain operations than objects/arrays.
3.11.3 Example
1 const map = new Map();
2 [Link](’name’, ’John’);
3 [Link](1, ’one’);
4 [Link]([Link](’name’)); // Output: John
5 [Link]([Link](1)); // Output: one
6
3.12.2 Explanation
Template literals use backticks for strings, supporting embedded expressions
and multiline strings, improving readability over concatenation.
3.12.3 Example
1 const name = ’Alice’;
2 const greeting = ‘Hello, ${name}!
3 Welcome to JavaScript.‘;
4 [Link](greeting);
5 // Output:
6 // Hello, Alice!
7 // Welcome to JavaScript.
3.13 Generators
3.13.1 Question
What are generators, and how do they work?
3.13.2 Explanation
Generators (function*, yield) pause and resume execution, useful for itera-
tors and async flows. They return an iterator with next().
3.13.3 Example
1 function* generator() {
2 yield ’Hello’;
3 yield ’World’;
4 }
5 const gen = generator();
6 [Link]([Link]().value); // Output: Hello
7 [Link]([Link]().value); // Output: World
8 [Link]([Link]().value); // Output: undefined
3.14.2 Explanation
Objects inherit properties from other objects via the prototype chain, enabling
code reuse. It’s a core feature distinguishing JavaScript from class-based lan-
guages.
3.14.3 Example
1 const parent = {
2 greet() {
3 [Link](’Hello from parent’);
4 }
5 };
6 const child = [Link](parent);
7 [Link](); // Output: Hello from parent
3.15.2 Explanation
Strict mode (”use strict”) enforces stricter parsing, preventing undeclared
variables and other errors. It’s used to write safer code.
3.15.3 Example
1 ’use strict’;
2 x = 10; // ReferenceError: x is not defined
3 function strictFunction() {
4 y = 20; // ReferenceError: y is not defined
5 }
3.16.2 Explanation
Debouncing delays function execution until a specified time passes; throttling
limits execution to once per interval. Both optimize performance for frequent
events.
3.16.3 Example
1 function debounce(func, wait) {
2 let timeout;
3 return function(...args) {
4 clearTimeout(timeout);
5 timeout = setTimeout(() => func(...args), wait);
6 };
7 }
8 const log = debounce(() => [Link](’Debounced!’), 1000);
9 log();
10
15 func(...args);
16 inThrottle = true;
17 setTimeout(() => (inThrottle = false), limit);
18 }
19 };
20 }
21 const throttledLog = throttle(() => [Link](’Throttled!’),
1000);
22 throttledLog();
3.17.1 Question
What are Web Workers?
3.17.2 Explanation
Web Workers run scripts in background threads, enabling parallel execution
without blocking the main thread, ideal for CPU-intensive tasks.
3.17.3 Example
1 // [Link]
2 [Link] = function(e) {
3 const result = [Link] * 2;
4 [Link](result);
5 };
6
7 // [Link]
8 const worker = new Worker(’[Link]’);
9 [Link] = e => [Link]([Link]); // Output: 10
10 [Link](5);
3.18.1 Question
How does JavaScript handle memory management and garbage collection?
30 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS
3.18.2 Explanation
JavaScript uses mark-and-sweep garbage collection to reclaim memory from un-
referenced objects. Developers assist by nullifying references and using WeakMap/WeakSet.
3.18.3 Example
1 let obj = { name: ’John’ };
2 obj = null; // Eligible for garbage collection
3
3.19.1 Question
What is functional programming in JavaScript?
3.19.2 Explanation
Functional programming uses pure functions, immutability, and composition for
predictable code. Methods like map, filter, and reduce support this paradigm.
3.19.3 Example
1 const add = (a, b) => a + b;
2 const double = x => x * 2;
3 const square = x => x * x;
4 const doubleThenSquare = x => square(double(x));
5 [Link](doubleThenSquare(3)); // Output: 36
3.20.1 Question
What is the difference between == and ===?
3.21. IIFE (IMMEDIATELY INVOKED FUNCTION EXPRESSION) 31
3.20.2 Explanation
== performs type coercion; === checks value and type without coercion. [Link]
handles edge cases like NaN.
3.20.3 Example
1 [Link](5 == ’5’); // Output: true
2 [Link](5 === ’5’); // Output: false
3 [Link]([Link](NaN, NaN)); // Output: true
3.21.1 Question
What is an IIFE, and why is it used?
3.21.2 Explanation
An IIFE is a function executed immediately after definition, used for data privacy
and avoiding global scope pollution.
3.21.3 Example
1 (function() {
2 const privateVar = ’I\’m private’;
3 [Link](privateVar); // Output: I’m private
4 })();
5 [Link](typeof privateVar); // Output: undefined
3.22.1 Question
What are common JavaScript design patterns?
3.22.2 Explanation
Patterns like Module Pattern, Singleton, and Observer provide reusable solu-
tions. The Module Pattern is common for encapsulation.
32 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS
3.22.3 Example
1 const Module = (function() {
2 let privateVar = ’I\’m private’;
3 return {
4 getVar() {
5 return privateVar;
6 }
7 };
8 })();
9 [Link]([Link]()); // Output: I’m private
10 [Link](typeof privateVar); // Output: undefined
3.23.2 Explanation
Higher-order functions take or return functions, enabling patterns like mapping
or filtering. They are key to functional programming.
3.23.3 Example
1 function higherOrder(fn) {
2 return function(...args) {
3 [Link](’Executing function’);
4 return fn(...args);
5 };
6 }
7
3.24 Currying
3.24.1 Question
What is currying in JavaScript?
3.25. EVENT DELEGATION 33
3.24.2 Explanation
Currying transforms a function with multiple arguments into a sequence of single-
argument functions, useful for partial application.
3.24.3 Example
1 function curry(fn) {
2 return function curried(...args) {
3 if ([Link] >= [Link]) {
4 return fn(...args);
5 }
6 return (...nextArgs) => curried(...args, ...nextArgs);
7 };
8 }
9
3.25.1 Question
What is event delegation, and how is it used?
3.25.2 Explanation
Event delegation uses event bubbling to handle events at a higher-level element,
reducing listeners and improving performance for dynamic elements.
3.25.3 Example
1 [Link](’#list’).addEventListener(’click’, e => {
2 if ([Link] === ’LI’) {
3 [Link](‘Clicked: ${[Link]}‘);
4 }
5 });
6
3.26 Polyfills
3.26.1 Question
What are polyfills, and how are they used?
3.26.2 Explanation
Polyfills implement missing features for older browsers, ensuring compatibility.
They are common in interviews to test feature implementation.
3.26.3 Example
1 if (![Link]) {
2 [Link] = function(value) {
3 return [Link](value) !== -1;
4 };
5 }
6
3.27.2 Explanation
Type coercion is the automatic conversion of values during operations (e.g., ==).
It can lead to unexpected results, so === is preferred.
3.27.3 Example
1 [Link](’5’ + 1); // Output: ’51’ (string concatenation)
2 [Link](’5’ - 1); // Output: 4 (numeric conversion)
3 [Link](5 == ’5’); // Output: true (coercion)
4 [Link](5 === ’5’); // Output: false (no coercion)
35