[Go to site: main page, start]

0% found this document useful (0 votes)
3 views35 pages

JavaScript Interview Guide 2025

Mastering JavaScript: The Ultimate Interview Guide for 2025 is a comprehensive resource designed to help candidates excel in JavaScript interviews. It includes detailed explanations, practical examples, and interview tips across various topics such as fundamentals, asynchronous JavaScript, and advanced concepts. The guide is structured to cover essential JavaScript knowledge and skills needed for success in interviews.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

JavaScript Interview Guide 2025

Mastering JavaScript: The Ultimate Interview Guide for 2025 is a comprehensive resource designed to help candidates excel in JavaScript interviews. It includes detailed explanations, practical examples, and interview tips across various topics such as fundamentals, asynchronous JavaScript, and advanced concepts. The guide is structured to cover essential JavaScript knowledge and skills needed for success in interviews.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mastering JavaScript: The Ultimate

Interview Guide for 2025

Created by Vishal Kanojiya

July 2025

A comprehensive resource for acing JavaScript interviews with detailed


explanations, practical examples, and diagrams.
Contents

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

2.2.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16


2.3 Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3 Advanced JavaScript Concepts 19


3.1 Objects and Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1.4 Visual Aid: Prototype Chain . . . . . . . . . . . . . . . . . . . . 20
3.1.5 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 Arrays and Array Methods . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Event Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.4 Visual Aid: Event Loop . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.5 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4 ES6+ Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.5 Data Structures (Array as Stack/Queue) . . . . . . . . . . . . . . . . . . 22
3.5.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.5.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.5.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.5.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.6 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.6.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.6.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.6.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.6.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.7 Modules (ES6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.7.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.7.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.7.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.7.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.8 Hoisting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.8.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.8.2 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4 CONTENTS

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

3.27.4 Interview Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

4 Conclusion 35
Preface

This e-book, authored by Vishal Kanojiya, is designed for developers preparing


for JavaScript interviews in 2025. It covers core and advanced JavaScript con-
cepts, including variables, scopes, closures, ES6+ features, asynchronous pro-
gramming, functional programming, design patterns, and more. Each topic in-
cludes a question, an in-depth explanation, practical use cases, and complete, VS
Code-style code examples. Diagrams and tables clarify complex concepts like the
event loop and prototypal inheritance. The clickable table of contents ensures
easy navigation. With over 50 pages of content, this guide equips you to excel in
technical interviews.

7
8 CONTENTS
1 Fundamentals of JavaScript

1.1 Variables and Data Types

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

7 // Using let: block-scoped


8 let y = 10;
9 // let y = 20; // Error: Cannot redeclare
10 y = 30; // Reassignable
11 [Link](y); // Output: 30
12

13 // Using const: block-scoped, immutable binding


14 const z = 10;
15 // z = 20; // Error: Assignment to constant
16 const obj = { name: ’John’ };
17 [Link] = ’Jane’; // Allowed: modifying properties
18 [Link](obj); // Output: { name: ’Jane’ }

9
10 CHAPTER 1. FUNDAMENTALS OF JAVASCRIPT

1.1.4 Interview Tip


Interviewers often ask about hoisting or scoping issues (e.g., var vs. let). Be
prepared to explain Temporal Dead Zone (TDZ) for let and const.

1.2 Scopes and Lexical Scope

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

11 let globalVar = ’I\’m global’;


12 function checkScope() {
13 [Link](globalVar); // Accessible
14 }
15 checkScope(); // Output: I’m global

1.2.4 Interview Tip


Explain how lexical scope enables closures and why block scope (let, const)
prevents common bugs like loop variable leaks.

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

12 // Example: Function factory


13 function greet(name) {
14 return function() {
15 [Link](‘Hello, ${name}!‘);
16 };
17 }
18 const greetJohn = greet(’John’);
19 greetJohn(); // Output: Hello, John!

1.3.4 Interview Tip


Be ready to explain how closures maintain state and provide an example like a
counter or module pattern.

1.4 Functions and Arrow Functions


1.4.1 Question
What are the differences between regular functions and arrow functions in ES6?

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

5 const arrowFunction = () => {


6 [Link](this); // Inherits surrounding this
7 };
8

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

17 const add = (a, b) => a + b;


18 [Link](add(2, 3)); // Output: 5

1.4.4 Interview Tip


Discuss how arrow functions simplify syntax but can cause issues with this in
event handlers or object methods.

1.5 Conditional Statements


1.5.1 Question
How do you use conditional statements in JavaScript?

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

1.5.4 Interview Tip


Explain short-circuit evaluation (e.g., , ||) and how switch can optimize multi-
ple conditions.
14 CHAPTER 1. FUNDAMENTALS OF JAVASCRIPT
2 Asynchronous JavaScript

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));

2.1.4 Interview Tip


Demonstrate Promise chaining and error handling, and compare Promises to
callbacks.

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.2.4 Interview Tip


Explain error handling with try/catch and how async/await improves read-
ability over Promise chains.

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

11 // Nested callbacks example


12 function fetchData(callback) {
13 setTimeout(() => {
14 callback(’Step 1’);
15 }, 1000);
16 }
17 fetchData(data => {
18 [Link](data); // Output: Step 1
19 fetchData(next => [Link](next)); // Output: Step 1
20 });

2.3.4 Interview Tip


Discuss callback hell and how modern JavaScript (Promises, async/await) ad-
dresses it.
18 CHAPTER 2. ASYNCHRONOUS JAVASCRIPT
3 Advanced JavaScript Concepts

3.1 Objects and Prototypes

3.1.1 Question

How do you create and manipulate objects, and what is prototypal inheritance?

3.1.2 Explanation

Objects store key-value pairs, created via literals, constructors, or [Link]().


Prototypal inheritance allows objects to inherit properties via the prototype chain,
a core JavaScript feature tested in interviews.

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

3.1.4 Visual Aid: Prototype Chain

dog
[[Prototype]]
[Link]
[[Prototype]]
[Link]

3.1.5 Interview Tip


Explain the prototype chain and how [Link]() differs from construc-
tor functions.

3.2 Arrays and Array Methods


3.2.1 Question
What are common ES6 array methods, and how are they used?

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 // map: Transform elements


4 const doubled = [Link](num => num * 2);
5 [Link](doubled); // Output: [2, 4, 6, 8]
6

7 // filter: Select elements


8 const evens = [Link](num => num % 2 === 0);
9 [Link](evens); // Output: [2, 4]
10

11 // reduce: Aggregate values


12 const sum = [Link]((acc, curr) => acc + curr, 0);
13 [Link](sum); // Output: 10
14

15 // find: Return first match


16 const found = [Link](num => num > 2);
17 [Link](found); // Output: 3

3.2.4 Interview Tip


Be prepared to write polyfills for array methods or solve problems like filtering
unique values.
3.3. EVENT LOOP 21

3.3 Event Loop


3.3.1 Question
What is the event loop, and how does it handle asynchronous tasks?

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.3.4 Visual Aid: Event Loop

Call Stack Task Queue

Empty? Microtask Queue


Process
Process
Event Loop

3.3.5 Interview Tip


Explain the priority of microtasks over tasks and provide an example with mixed
async operations.

3.4 ES6+ Features


3.4.1 Question
What are key ES6+ features, and how do they improve JavaScript?

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

4 const arr1 = [1, 2];


5 const arr2 = [...arr1, 3, 4];
6 [Link](arr2); // Output: [1, 2, 3, 4]
7

8 const user = ’Alice’;


9 [Link](‘Hello, ${user}!‘); // Output: Hello, Alice!
10

11 function greet(name = ’Guest’) {


12 return ‘Hi, ${name}‘;
13 }
14 [Link](greet()); // Output: Hi, Guest
15

16 const obj = { user: { name: ’John’ } };


17 [Link]([Link]?.name); // Output: John

3.4.4 Interview Tip


Discuss how optional chaining prevents errors and how modules improve code
organization.

3.5 Data Structures (Array as Stack/Queue)


3.5.1 Question
How can you implement a stack or queue using arrays?

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

6 const queue = [];


7 [Link](1); // Add to end
8 [Link](2);
9 [Link]([Link]()); // Output: 1 (FIFO)

3.5.4 Interview Tip


Explain time complexity: push/pop (O(1)), shift (O(n)).
3.6. ERROR HANDLING 23

3.6 Error Handling


3.6.1 Question
How do you handle errors in JavaScript?

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

7 async function fetchData() {


8 try {
9 await [Link](’Failed’);
10 } catch (error) {
11 [Link](error); // Output: Failed
12 }
13 }
14 fetchData();

3.6.4 Interview Tip


Discuss custom error classes and graceful error handling in APIs.

3.7 Modules (ES6)


3.7.1 Question
How do ES6 modules work in JavaScript?

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

6 import { add, subtract } from ’./[Link]’;


7 [Link](add(5, 3)); // Output: 8
8 [Link](subtract(5, 3)); // Output: 2

3.7.4 Interview Tip


Compare ES6 modules to CommonJS and explain tree-shaking benefits.

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

4 [Link](y); // ReferenceError: y is not defined


5 let y = 20;
6

7 function hoisted() {
8 [Link](’Hoisted!’);
9 }
10 hoisted(); // Output: Hoisted!

3.8.4 Interview Tip


Explain TDZ and why let/const prevent hoisting-related bugs.

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

4 const { name, age } = { name: ’Alice’, age: 25 };


5 [Link](name, age); // Output: Alice 25
6

7 function printPerson({ name, age }) {


8 [Link](‘Name: ${name}, Age: ${age}‘);
9 }
10 printPerson({ name: ’Bob’, age: 30 }); // Output: Name: Bob, Age: 30

3.9.4 Interview Tip


Show how destructuring simplifies function arguments and nested object access.

3.10 Spread and Rest Operators

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.10.4 Interview Tip


Explain use cases like object merging and variable-length arguments.
26 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS

3.11 Map and Set


3.11.1 Question
What are Map and Set in ES6?

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

7 const set = new Set([1, 2, 2, 3]);


8 [Link]([...set]); // Output: [1, 2, 3]

3.11.4 Interview Tip


Compare Map to objects and Set to arrays for unique values.

3.12 Template Literals


3.12.1 Question
What are template literals, and how are they used?

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.12.4 Interview Tip


Show tagged template literals for advanced string processing.
3.13. GENERATORS 27

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.13.4 Interview Tip


Explain use cases like custom iterators or handling large datasets.

3.14 Prototypal Inheritance


3.14.1 Question
What is prototypal inheritance in JavaScript?

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.14.4 Interview Tip


Compare prototypal inheritance to classical inheritance.
28 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS

3.15 Strict Mode


3.15.1 Question
What is strict mode in JavaScript?

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.15.4 Interview Tip


Discuss specific errors caught by strict mode, like silent failures.

3.16 Debouncing and Throttling


3.16.1 Question
What are debouncing and throttling?

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

11 function throttle(func, limit) {


12 let inThrottle;
13 return function(...args) {
14 if (!inThrottle) {
3.17. WEB WORKERS 29

15 func(...args);
16 inThrottle = true;
17 setTimeout(() => (inThrottle = false), limit);
18 }
19 };
20 }
21 const throttledLog = throttle(() => [Link](’Throttled!’),
1000);
22 throttledLog();

3.16.4 Interview Tip


Provide use cases like search inputs (debouncing) or scroll events (throttling).

3.17 Web Workers

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.17.4 Interview Tip


Discuss limitations, like no DOM access in workers.

3.18 Memory Management and Garbage Collection

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

4 const weakMap = new WeakMap();


5 let key = {};
6 [Link](key, ’Value’);
7 key = null; // Key and value can be garbage collected
8 [Link]([Link](key)); // Output: false

3.18.4 Interview Tip


Explain memory leaks from event listeners and how WeakMap helps.

3.19 Functional Programming

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.19.4 Interview Tip


Discuss pure functions and avoiding side effects.

3.20 Equality Operators (== vs ===)

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.20.4 Interview Tip


Explain coercion pitfalls and why === is preferred.

3.21 IIFE (Immediately Invoked Function Expression)

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.21.4 Interview Tip


Compare IIFEs to ES6 modules for encapsulation.

3.22 Design Patterns

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.22.4 Interview Tip


Discuss when to use patterns like Singleton for single-instance objects.

3.23 Higher-Order Functions


3.23.1 Question
What are higher-order functions in JavaScript?

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

8 const add = (a, b) => a + b;


9 const enhancedAdd = higherOrder(add);
10 [Link](enhancedAdd(2, 3)); // Output: Executing function, 5

3.23.4 Interview Tip


Provide examples like map or custom higher-order functions.

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

10 const add = (a, b, c) => a + b + c;


11 const curriedAdd = curry(add);
12 [Link](curriedAdd(1)(2)(3)); // Output: 6

3.24.4 Interview Tip


Explain use cases like reusable function templates.

3.25 Event Delegation

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

7 // HTML: <ul id=”list”><li>Item 1</li><li>Item 2</li></ul>


8 // Clicking an <li> logs its content

3.25.4 Interview Tip


Discuss performance benefits and dynamic element handling.
34 CHAPTER 3. ADVANCED JAVASCRIPT CONCEPTS

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

7 const arr = [1, 2, 3];


8 [Link]([Link](2)); // Output: true

3.26.4 Interview Tip


Write a polyfill for methods like map or bind.

3.27 Type Coercion


3.27.1 Question
What is type coercion in JavaScript?

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)

3.27.4 Interview Tip


Explain coercion rules (e.g., ToPrimitive) and edge cases like null == undefined.
4 Conclusion

This e-book, authored by Vishal Kanojiya, provides a thorough guide to master-


ing JavaScript for 2025 interviews. With detailed explanations, practical exam-
ples, diagrams, and interview tips, it ensures you’re well-prepared for technical
challenges. Practice these concepts to excel in your interviews!

35

You might also like