JS Chapter 1-5

Perfect. For JavaScript, I'll follow the same style that worked best for the SQL chapters:

  • 4 scenario-based questions per chapter.

  • Every question starts with "Suppose...".

  • Every answer contains code + output + explanation.

  • By the end of 4 questions, you'll know 100% of that chapter.


Chapter 1 — Introduction to JavaScript


Q1. Suppose your manager says:

"Print Hello, World! on the screen. How will you do it?"

Answer

console.log("Hello, World!");

Output

Hello, World!

Explanation

console.log() prints data to the console.

Think of it as JavaScript's print().


Q2. Suppose your manager asks:

"What exactly is JavaScript?"

Answer

JavaScript is a programming language used to make websites interactive.

Example:

<button onclick="alert('Button Clicked!')">
Click Me
</button>

Output

(Button Clicked!)

Without JavaScript,

  • No dropdowns

  • No animations

  • No form validation

  • No games

  • No dynamic websites


Q3. Suppose your interviewer asks:

"Where can JavaScript run?"

Answer

JavaScript mainly runs in two places.

Browser

console.log("Running inside Chrome");

Example

  • Chrome

  • Firefox

  • Edge


Node.js

console.log("Running on Server");

Example

node app.js

Output

Running on Server

Now JavaScript can build

  • Servers

  • APIs

  • CLI Tools

  • Desktop Apps


Q4. Suppose your interviewer asks:

"How does JavaScript actually execute?"

Answer

Flow

JavaScript Code

↓

JavaScript Engine

↓

Machine Code

↓

CPU

Example

console.log(10 + 20);

Output

30

Popular JavaScript Engines

EngineUsed In
V8Chrome, Node.js
SpiderMonkeyFirefox
JavaScriptCoreSafari

Concepts Covered ✅

  • JavaScript

  • console.log()

  • Browser

  • Node.js

  • JavaScript Engine

  • V8

  • First Program


Chapter 2 — Variables & Data Types


Q1. Suppose your manager says:

"Store a user's name and age."

Answer

let name = "Rahul";
let age = 25;

console.log(name);
console.log(age);

Output

Rahul
25

Variables store values.


Q2. Suppose your interviewer asks:

"Difference between var, let and const?"

Answer

var city = "Delhi";

let country = "India";

const pi = 3.14;

Output

city → can change

country → can change

pi → cannot change

Trying

const pi = 3.14;

pi = 5;

Output

TypeError

Q3. Suppose your manager says:

"Store different kinds of values."

Answer

let name = "Rahul";

let age = 25;

let isStudent = true;

let salary = null;

let company;

Output

Rahul

25

true

null

undefined

Primitive Data Types

  • String

  • Number

  • Boolean

  • Null

  • Undefined

  • BigInt

  • Symbol


Q4. Suppose interviewer asks:

"How do you know the data type?"

Answer

Use

typeof

Example

console.log(typeof "Hello");

console.log(typeof 10);

console.log(typeof true);

console.log(typeof undefined);

Output

string

number

boolean

undefined

Special Case

console.log(typeof null);

Output

object

(It is a famous JavaScript bug kept for backward compatibility.)


Concepts Covered ✅

  • Variables

  • var

  • let

  • const

  • Primitive Types

  • typeof

  • undefined

  • null


Chapter 3 — Operators


Q1. Suppose your manager says:

"Calculate total price."

Answer

let price = 200;

let tax = 50;

console.log(price + tax);

Output

250

Arithmetic Operators

+

-

*

/

%

**

Q2. Suppose your interviewer asks:

"Compare two ages."

Answer

let age = 20;

console.log(age > 18);

console.log(age == 20);

console.log(age === 20);

Output

true

true

true

Comparison Operators

>

<

>=

<=

==

===

Q3. Suppose your manager says:

"User can login only if email and password are correct."

Answer

let email = true;

let password = true;

console.log(email && password);

Output

true

Logical Operators

&&

||

!

Q4. Suppose interviewer asks:

"Difference between == and ===?"

Answer

console.log(5 == "5");

Output

true

Because

Only value is compared.

Now

console.log(5 === "5");

Output

false

Because

Value

AND

Type

Both are checked.

Always prefer

===

Concepts Covered ✅

  • Arithmetic

  • Assignment

  • Comparison

  • Logical

  • ==

  • ===

  • Operator basics


Chapter 4 — Type Conversion


Q1. Suppose your manager says:

"Convert a string into a number."

Answer

let age = "25";

console.log(Number(age));

Output

25

Type

console.log(typeof Number(age));

Output

number

Q2. Suppose user enters

10
20

How do you add them correctly?

Answer

Without conversion

let a = "10";

let b = "20";

console.log(a + b);

Output

1020

Correct

console.log(Number(a) + Number(b));

Output

30

Q3. Suppose interviewer asks:

"Difference between Truthy and Falsy?"

Answer

Falsy Values

false

0

""

null

undefined

NaN

Example

if ("") {
    console.log("Hello");
}
else {
    console.log("Empty");
}

Output

Empty

Q4. Suppose interviewer asks:

"Difference between parseInt() and Number()?"

Answer

console.log(parseInt("25px"));

Output

25

Now

console.log(Number("25px"));

Output

NaN

Concepts Covered ✅

  • Number()

  • String()

  • Boolean()

  • parseInt()

  • parseFloat()

  • Truthy

  • Falsy

  • NaN


Chapter 5 — Strings


Q1. Suppose your manager says:

"Create a welcome message using user's name."

Answer

Old way

let name = "Rahul";

console.log("Welcome " + name);

Modern way

let name = "Rahul";

console.log(`Welcome ${name}`);

Output

Welcome Rahul

Template Literals use backticks ( ) and ${} placeholders.


Q2. Suppose you need to find the length of a username and convert it to uppercase.

Answer

let username = "rahul";

console.log(username.length);

console.log(username.toUpperCase());

Output

5

RAHUL

Other useful methods

username.toLowerCase()

username.trim()

Q3. Suppose your manager says:

"Check whether the email contains '@gmail.com'."

Answer

let email = "rahul@gmail.com";

console.log(email.includes("@gmail.com"));

Output

true

Find position

console.log(email.indexOf("@"));

Output

5

Q4. Suppose interviewer asks:

"Extract first name from 'Rahul Sharma'."

Answer

let name = "Rahul Sharma";

console.log(name.slice(0, 5));

Output

Rahul

Split the string

console.log(name.split(" "));

Output

["Rahul", "Sharma"]

Replace text

console.log(name.replace("Rahul", "Aman"));

Output

Aman Sharma

Concepts Covered ✅

  • String creation

  • Template literals

  • ${}

  • .length

  • .toUpperCase()

  • .toLowerCase()

  • .trim()

  • .includes()

  • .indexOf()

  • .slice()

  • .split()

  • .replace()


🎯 After Chapters 1–5, you can:

  • Write and run JavaScript programs.

  • Declare variables correctly with let and const.

  • Work confidently with numbers, strings, and booleans.

  • Use operators and understand == vs ===.

  • Convert between data types and recognize truthy/falsy values.

  • Manipulate strings using the most commonly used methods.

This gives you the foundation needed before moving on to control flow, loops, functions, objects, and asynchronous JavaScript.

No comments:

Post a Comment

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