JS Chapter 11 — 15

Chapter 11 — Arrays


Q1. Suppose you're building an E-commerce website.

Your manager says:

"Store multiple product names instead of creating 100 variables."

Answer

Without Array ❌

let p1 = "Laptop";
let p2 = "Mouse";
let p3 = "Keyboard";

Using Array ✅

let products = ["Laptop", "Mouse", "Keyboard"];

console.log(products);

Output

["Laptop", "Mouse", "Keyboard"]

Accessing elements

console.log(products[0]);
console.log(products[2]);

Output

Laptop
Keyboard

Arrays use 0-based indexing.


Q2. Suppose your manager says:

"A new product arrived. Add it to the list."

Answer

let products = ["Laptop", "Mouse"];

products.push("Keyboard");

console.log(products);

Output

["Laptop", "Mouse", "Keyboard"]

Remove last product

products.pop();

console.log(products);

Output

["Laptop", "Mouse"]

Concepts

push() → Add at end

pop() → Remove from end

Q3. Suppose the first product becomes unavailable.

Your manager says:

"Remove the first product and add a new one at the beginning."

Answer

let products = ["Laptop", "Mouse", "Keyboard"];

products.shift();

console.log(products);

products.unshift("Tablet");

console.log(products);

Output

["Mouse", "Keyboard"]

["Tablet", "Mouse", "Keyboard"]

Concepts

shift() → Remove first

unshift() → Add first

Q4. Suppose interviewer asks:

"Difference between slice() and splice()?"

Answer

let arr = [10,20,30,40,50];

console.log(arr.slice(1,4));

Output

[20,30,40]

Original Array

[10,20,30,40,50]

Now

arr.splice(1,2);

console.log(arr);

Output

[10,40,50]

Difference

slice()splice()
Doesn't modify original arrayModifies original array
Returns copied portionRemoves/Adds elements

Concepts Covered ✅

  • Arrays

  • Index

  • push()

  • pop()

  • shift()

  • unshift()

  • slice()

  • splice()

  • Accessing elements


Chapter 12 — Advanced Array Methods ⭐⭐⭐

Assume

let nums = [10,20,30,40,50];

Q1. Suppose your manager says:

"Increase every salary by ₹1000."

Answer

Use map()

let salary = [10000,20000,30000];

let updated = salary.map(s => s + 1000);

console.log(updated);

Output

[11000,21000,31000]

Why map?

Creates a new array.

Original remains unchanged.


Q2. Suppose manager says:

"Show only employees earning more than ₹20,000."

Answer

let salary = [10000,25000,15000,40000];

let result = salary.filter(s => s > 20000);

console.log(result);

Output

[25000,40000]

Why filter?

Keeps only matching values.


Q3. Suppose manager says:

"Calculate total sales."

Answer

let sales = [100,200,300,400];

let total = sales.reduce((sum,current)=>{

return sum + current;

},0);

console.log(total);

Output

1000

Why reduce?

Reduces many values into one.


Q4. Suppose interviewer asks:

"Difference among map(), filter(), find(), some(), every(), forEach()?"

Answer

let nums=[10,20,30,40];

Find first value

console.log(nums.find(n=>n>20));

Output

30

Check if any value >30

console.log(nums.some(n=>n>30));

Output

true

Check all values >5

console.log(nums.every(n=>n>5));

Output

true

Loop

nums.forEach(n=>console.log(n));

Output

10

20

30

40

Summary

MethodReturns
map()New transformed array
filter()Matching elements
reduce()Single value
find()First matching element
some()true/false
every()true/false
forEach()Nothing (just loops)

Concepts Covered ✅

  • map()

  • filter()

  • reduce()

  • find()

  • some()

  • every()

  • forEach()

  • Functional Programming Basics


Chapter 13 — Scope & Closures ⭐⭐⭐


Q1. Suppose your manager says:

"The variable salary should only be accessible inside a function."

Answer

function employee(){

let salary = 50000;

console.log(salary);

}

employee();

Output

50000

Now

console.log(salary);

Output

ReferenceError

This is Local Scope.


Q2. Suppose interviewer asks:

"Difference between Global Scope and Block Scope?"

Answer

let company = "Google";

if(true){

let city = "Delhi";

}

console.log(company);

console.log(city);

Output

Google

ReferenceError

Scope Types

ScopeAccessible Where?
GlobalEverywhere
FunctionInside function
BlockInside {}

Q3. Suppose interviewer asks:

"What is Closure?"

Answer

function outer(){

let count = 0;

return function(){

count++;

console.log(count);

}

}

let counter = outer();

counter();

counter();

counter();

Output

1

2

3

Why?

Inner function remembers variables from outer function even after outer has finished.

That's Closure.


Q4. Suppose interviewer asks:

"Real-world use of Closure?"

Answer

Private Counter

function bank(){

let balance = 1000;

return {

check(){

console.log(balance);

},

deposit(amount){

balance += amount;

}

}

}

let account = bank();

account.deposit(500);

account.check();

Output

1500

No one can directly do

balance = 0;

Closure provides Data Privacy.


Concepts Covered ✅

  • Global Scope

  • Local Scope

  • Block Scope

  • Lexical Scope

  • Closures

  • Data Hiding

  • Private Variables


Chapter 14 — Execution Context & Hoisting ⭐⭐⭐


Q1. Suppose interviewer asks:

"Why does this work?"

sayHello();

function sayHello(){

console.log("Hello");

}

Output

Hello

Answer

Function Declarations are Hoisted.

JavaScript moves them to memory before execution.


Q2. Suppose interviewer asks:

"Why does this fail?"

console.log(age);

let age = 22;

Output

ReferenceError

Why?

Because of

Temporal Dead Zone (TDZ)

let and const exist but can't be used before declaration.


Q3. Suppose interviewer asks:

"Difference between var and let hoisting?"

Answer

console.log(a);

var a = 10;

Output

undefined

Now

console.log(b);

let b = 10;

Output

ReferenceError

Q4. Suppose interviewer asks:

"Explain Execution Context."

Answer

When JavaScript runs

let x = 10;

let y = 20;

console.log(x+y);

JavaScript performs

1.

Memory Creation Phase

↓

2.

Execution Phase

During memory phase

Variables allocated

Functions stored

During execution

Assignments happen

Statements execute

Concepts Covered ✅

  • Execution Context

  • Memory Phase

  • Execution Phase

  • Hoisting

  • TDZ

  • var Hoisting

  • let Hoisting

  • Function Hoisting


Chapter 15 — Callbacks


Q1. Suppose your manager says:

"Download a file. After download completes, process it."

Answer

function download(callback){

console.log("Downloading...");

callback();

}

download(function(){

console.log("Processing File");

});

Output

Downloading...

Processing File

A function passed into another function is called a Callback.


Q2. Suppose you're building a Login System.

Manager says:

"Login first. Then load dashboard."

Answer

function login(callback){

console.log("Login Success");

callback();

}

function dashboard(){

console.log("Dashboard Loaded");

}

login(dashboard);

Output

Login Success

Dashboard Loaded

Q3. Suppose your project becomes like this.

Answer

login(function(){

loadProfile(function(){

loadPosts(function(){

loadComments(function(){

console.log("Done");

});

});

});

});

This is called

Callback Hell

Problems

  • Difficult to read

  • Difficult to debug

  • Difficult to maintain

Promises solve this problem.


Q4. Suppose interviewer asks:

"When do we use callbacks?"

Answer

Whenever an operation finishes later, and another operation should start after it.

Examples:

  • Reading files

  • API calls

  • Database queries

  • Button click events

  • Timers (setTimeout)

  • User authentication

Example with setTimeout:

console.log("Start");

setTimeout(() => {
    console.log("Task Finished");
}, 2000);

console.log("End");

Output

Start
End
Task Finished

Why?

setTimeout is asynchronous. JavaScript doesn't wait—it continues executing the next statement and runs the callback after the timer completes.


Concepts Covered ✅

  • Callback Functions

  • Passing Functions as Arguments

  • Callback Execution

  • Asynchronous Programming Basics

  • Callback Hell

  • Real-world Callback Use Cases

  • Introduction to setTimeout


🎯 After Chapters 11–15, you've reached intermediate JavaScript.

You now understand:

  • Arrays and their most common operations.

  • Powerful array methods (map, filter, reduce, etc.).

  • Scope and closures—two of the most important interview topics.

  • Execution context, hoisting, and the Temporal Dead Zone.

  • Callback functions and why asynchronous programming needs them.

The next chapters (16–18) will cover Promises, Async/Await, and the Event Loop—the core concepts behind modern asynchronous JavaScript.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.