JS Chapter 26 — 30

Chapter 26 — ES6+ Features ⭐⭐⭐

Goal: Learn the modern JavaScript features you'll use every day.


Q1. Suppose your manager says:

"Copy one user's data into another object and update only the city."

Answer

const user = {
    name: "Rahul",
    age: 25,
    city: "Delhi"
};

const updatedUser = {
    ...user,
    city: "Bangalore"
};

console.log(updatedUser);

Output

{
  name: "Rahul",
  age: 25,
  city: "Bangalore"
}

Why?

...

is called the Spread Operator.

It copies arrays and objects.


Q2. Suppose a function accepts any number of marks.

Answer

function total(...marks){

    console.log(marks);

}

total(80,90,95,88);

Output

[80,90,95,88]

Why?

...

inside parameters

=

Rest Operator.

Spread

const arr2 = [...arr1];

Rest

function demo(...args){}

Q3. Suppose manager says:

"Extract only name and salary."

Answer

const employee={

name:"Rahul",

salary:50000,

city:"Delhi"

};

const {name,salary}=employee;

console.log(name);

console.log(salary);

Output

Rahul

50000

Array destructuring

const colors=["Red","Blue","Green"];

const [a,b]=colors;

console.log(a,b);

Output

Red Blue

Q4. Suppose interviewer asks:

"Optional Chaining vs Nullish Coalescing?"

Answer

const user={};

console.log(user.address?.city);

Output

undefined

No error.


Now

let salary = null;

console.log(salary ?? 10000);

Output

10000

Concepts Covered ✅

  • Spread (...)

  • Rest (...)

  • Object Destructuring

  • Array Destructuring

  • Template Literals

  • Optional Chaining (?.)

  • Nullish Coalescing (??)


Chapter 27 — Sets & Maps


Q1. Suppose your manager says:

"Remove duplicate IDs."

Answer

const ids=[1,2,2,3,3,4];

const unique=new Set(ids);

console.log(unique);

Output

Set(4){1,2,3,4}

Convert back

const result=[...unique];

console.log(result);

Output

[1,2,3,4]

Q2. Suppose manager says:

"Store unique skills."

Answer

const skills=new Set();

skills.add("Java");

skills.add("Python");

skills.add("Java");

console.log(skills);

Output

Set(2){

Java,

Python

}

Duplicate ignored.


Q3. Suppose you want to store

Employee ID → Employee Name

Answer

const employees=new Map();

employees.set(101,"Rahul");

employees.set(102,"Aman");

console.log(employees.get(101));

Output

Rahul

Q4. Suppose interviewer asks:

"Object vs Map vs Set?"

Answer

StructureStores
ObjectKey → Value
MapAny Key → Any Value
SetOnly Unique Values

Concepts Covered ✅

  • Set

  • add()

  • delete()

  • has()

  • Map

  • set()

  • get()

  • Unique Values


Chapter 28 — Date & Time


Q1. Suppose manager says:

"Print today's date."

Answer

const today=new Date();

console.log(today);

Output

2026-08-04T10:15:30...

(Output changes.)


Q2. Suppose you need only

Year

Month

Date

Answer

const today=new Date();

console.log(today.getFullYear());

console.log(today.getMonth()+1);

console.log(today.getDate());

Output

2026

8

4

Month starts from

0

January = 0


Q3. Suppose manager says:

"Calculate today's timestamp."

Answer

console.log(Date.now());

Output

1754300000000

Milliseconds since

Jan 1,1970

Q4. Suppose interviewer asks:

"Difference between Date() and Date.now()?"

Answer

new Date();

returns

Date Object.

Date.now();

returns

Timestamp.

Concepts Covered ✅

  • Date

  • Date.now()

  • getFullYear()

  • getMonth()

  • getDate()

  • Timestamp


Chapter 29 — Regular Expressions (Regex)


Q1. Suppose manager says:

"Check if phone number contains only digits."

Answer

const phone="9876543210";

const pattern=/^\d+$/;

console.log(pattern.test(phone));

Output

true

Q2. Suppose manager says:

"Validate an email."

Answer

const email="rahul@gmail.com";

const pattern=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;

console.log(pattern.test(email));

Output

true

Q3. Suppose manager says:

"Replace all spaces with '-'"

Answer

const text="I Love JavaScript";

console.log(text.replace(/\s/g,"-"));

Output

I-Love-JavaScript

Q4. Suppose interviewer asks:

"Difference between test(), match() and replace()?"

Answer

pattern.test(text)

Returns

true / false

text.match(pattern)

Returns

Matched values.


text.replace(pattern,newValue)

Returns

Updated String.

Concepts Covered ✅

  • Regex

  • test()

  • match()

  • replace()

  • Character Classes

  • Quantifiers

  • Validation


Chapter 30 — Memory Management & Garbage Collection


Q1. Suppose your manager says:

"Explain Stack and Heap."

Answer

let age=25;

let name="Rahul";

Primitive values

Stored in

Stack Memory

Objects

const user={

name:"Rahul"

};

Stored in

Heap Memory

Q2. Suppose manager says:

"Why did changing user2 also change user1?"

Answer

const user1={

name:"Rahul"

};

const user2=user1;

user2.name="Aman";

console.log(user1.name);

Output

Aman

Because

Both variables point to the

Same Object.


Q3. Suppose you no longer need an object.

Answer

let user={

name:"Rahul"

};

user=null;

Output

(No Output)

Object becomes unreachable.

Garbage Collector removes it automatically.


Q4. Suppose interviewer asks:

"What causes Memory Leaks?"

Answer

Common reasons

let users=[];

function addUser(){

users.push({

name:"Rahul"

});

}

Calling

addUser();

millions of times

Huge memory usage.

Other causes

  • Forgotten timers (setInterval)

  • Global variables

  • Detached DOM nodes

  • Event listeners never removed

Concepts Covered ✅

  • Stack Memory

  • Heap Memory

  • Primitive vs Reference Types

  • References

  • Garbage Collection

  • Reachability

  • Memory Leaks


🎉 JavaScript Course Complete (30 Chapters)

You now have a complete roadmap covering:

🟢 JavaScript Fundamentals

  • Variables & Data Types

  • Operators

  • Type Conversion

  • Strings

  • Numbers & Math

  • Control Flow

  • Loops

🟡 Intermediate JavaScript

  • Functions

  • Objects

  • Arrays

  • Array Methods

  • Scope & Closures

  • Hoisting

  • Callbacks

🔵 Asynchronous JavaScript

  • Promises

  • Async/Await

  • Event Loop

🟣 Browser JavaScript

  • DOM

  • Events

  • Forms

  • Fetch API

🟠 Professional JavaScript

  • Modules

  • Error Handling

  • OOP

🔴 Advanced JavaScript

  • ES6+ Features

  • Sets & Maps

  • Date & Time

  • Regex

  • Memory Management

This curriculum covers the core JavaScript knowledge expected for modern web development and provides a strong foundation before moving into frameworks like React or backend development with Node.js.

No comments:

Post a Comment

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