A Small Story Before We Start
Imagine you wake up on Monday morning.
Before even leaving your bed, your brain starts making decisions:
- If it is raining, carry an umbrella.
- If it is sunny, wear sunglasses.
- If you are late, skip breakfast.
- If you have enough time, make tea first.
Without realizing it, humans make thousands of decisions every day.
Now imagine a computer.
A computer is extremely fast.
A computer can perform billions of calculations.
But there is one thing it cannot do by itself:
Make decisions.
A computer only follows instructions.
That means if we want a program to behave differently in different situations, we must explicitly tell it what to do.
This is where Conditional Statements come into the picture.
Conditional statements allow JavaScript to think in a controlled way.
Not human thinking.
Not emotional thinking.
But logical decision-making.
Without conditional statements:
- Login systems would not work.
- ATM machines would not work.
- Food delivery apps would not work.
- E-commerce websites would not work.
- Banking applications would not work.
Almost every real-world application relies heavily on conditions.
What Is a Conditional Statement?
A conditional statement is a block of code that runs only when a specific condition is true.
Think about a security guard standing at a building entrance.
The guard follows a simple rule:
If the employee has an ID card, allow entry.
Otherwise:
Deny entry.
This is exactly how a conditional statement works.
Real Life Logic
IF person has ID card
Allow Entry
ELSE
Deny Entry
JavaScript Logic
let hasIdCard = true;
if (hasIdCard) {
console.log("Allow Entry");
} else {
console.log("Deny Entry");
}
Output:
Allow Entry
JavaScript checks the condition.
If the condition is true, it executes the first block.
If false, it executes the second block.
Why Conditional Statements Are Important
Imagine Amazon without conditions.
A user clicks "Buy Now."
What should happen?
JavaScript must check:
Is user logged in?
Is product available?
Is payment successful?
Is delivery location serviceable?
Each question is a condition.
Every answer affects the next action.
Without conditions, applications would behave the same for everyone, regardless of circumstances.
That would make software useless.
Understanding Boolean Values
Before understanding conditions, we must understand Booleans.
A Boolean can have only two values:
true
false
Examples:
let isLoggedIn = true;
let isAdmin = false;
let paymentDone = true;
Think of a switch.
ON = true
OFF = false
Conditional statements work using Boolean values.
The if Statement
The simplest conditional statement is the if statement.
Syntax:
if (condition) {
// code to execute
}
Example:
let age = 20;
if (age >= 18) {
console.log("You can vote");
}
Output:
You can vote
JavaScript evaluates:
20 >= 18
Result:
true
Therefore the code executes.
What Happens When Condition Is False?
Example:
let age = 15;
if (age >= 18) {
console.log("You can vote");
}
Output:
Nothing
The code inside the block is skipped.
This is important.
An if statement only runs when the condition is true.
Visualizing the Flow
Imagine a road checkpoint.
Start
|
Check Age >= 18 ?
|
+---- YES ----> Allow Voting
|
+---- NO -----> Stop
This is exactly how JavaScript processes conditions.
The if...else Statement
Most applications need two possible outcomes.
Example:
if (condition) {
do something
} else {
do something else
}
Real World Example:
let passwordCorrect = false;
if (passwordCorrect) {
console.log("Login Successful");
} else {
console.log("Invalid Password");
}
Output:
Invalid Password
ATM Machine Analogy
Imagine an ATM.
The ATM checks:
Does account have enough balance?
If yes:
Dispense Money
Otherwise:
Insufficient Balance
JavaScript version:
let balance = 500;
let withdrawal = 1000;
if (balance >= withdrawal) {
console.log("Cash Dispensed");
} else {
console.log("Insufficient Balance");
}
Output:
Insufficient Balance
This is exactly how banking software works internally.
The if...else if...else Statement
Real life decisions often have multiple possibilities.
Example:
A school grading system.
90+ = A
80+ = B
70+ = C
Below 70 = D
JavaScript provides:
if
else if
else
Example:
let marks = 85;
if (marks >= 90) {
console.log("Grade A");
}
else if (marks >= 80) {
console.log("Grade B");
}
else if (marks >= 70) {
console.log("Grade C");
}
else {
console.log("Grade D");
}
Output:
Grade B
How JavaScript Evaluates Multiple Conditions
Consider:
let marks = 95;
JavaScript starts from the top.
Step 1:
marks >= 90
Result:
true
As soon as JavaScript finds a true condition:
Grade A
is executed.
Then JavaScript stops checking the remaining conditions.
This improves performance.
Real World Example: Food Delivery App
Suppose a delivery app determines delivery charges.
let distance = 3;
Rules:
0-5 km → ₹30
5-10 km → ₹60
10-20 km → ₹100
20+ km → ₹150
Code:
if (distance <= 5) {
console.log("Delivery Charge ₹30");
}
else if (distance <= 10) {
console.log("Delivery Charge ₹60");
}
else if (distance <= 20) {
console.log("Delivery Charge ₹100");
}
else {
console.log("Delivery Charge ₹150");
}
Output:
Delivery Charge ₹30
Nested if Statements
Sometimes one decision depends on another decision.
Example:
A company wants to check:
Is employee active?
Then:
Is employee manager?
Code:
let active = true;
let manager = true;
if (active) {
if (manager) {
console.log("Access Management Dashboard");
}
}
Output:
Access Management Dashboard
This is called nesting.
One condition inside another condition.
Login System Example
Let's build simple login logic.
let email = "admin@gmail.com";
let password = "123456";
if (email === "admin@gmail.com") {
if (password === "123456") {
console.log("Login Successful");
} else {
console.log("Wrong Password");
}
} else {
console.log("Email Not Found");
}
Possible outputs:
Login Successful
or
Wrong Password
or
Email Not Found
This pattern is used in almost every authentication system.
Comparison Operators in Conditions
Conditions usually use comparison operators.
Equal To
==
Example:
5 == "5"
Output:
true
Because JavaScript converts data types automatically.
Strict Equal To
===
Example:
5 === "5"
Output:
false
Because:
Number ≠ String
Professional developers prefer: ===
because it avoids unexpected bugs.
Not Equal
!=
Example: 10 != 5
Output: true
Strict Not Equal
!==
Example: 10 !== "10"
Output: true
Greater Than
>
Example: 20 > 10
Output: true
Less Than
<
Example: 5 < 10
Output: true
Greater Than or Equal
>=
Example: 18 >= 18
Output: true
Less Than or Equal
<=
Example: 10 <= 20
Output: true
Common Beginner Mistake
Many beginners accidentally write:
if (age = 18)
instead of:
if (age === 18)
Difference: = means assignment.
===means comparison.
Always be careful.
This small mistake causes many bugs in real projects.
Decision Making in Real Applications
Conditional statements power:
Login Systems
if (userExists)
Payment Systems
if (paymentSuccess)
E-commerce
if (stockAvailable)
Banking
if (balanceEnough)
Ride Sharing Apps
if (driverAvailable)
Hospital Systems
if (doctorAvailable)
Almost every screen you interact with daily uses hundreds of conditions behind the scenes.
Summary
In this chapter, we learned how JavaScript makes decisions using conditional statements.
We covered:
- What conditional statements are
- Why applications need decision making
- Boolean values
-
ifstatements -
if...else -
if...else if...else - Nested conditions
- Comparison operators
- Login and ATM examples
- Common beginner mistakes
Think of conditional statements as the traffic police of your code.
Without them, every piece of code would run blindly.
With them, JavaScript can choose the correct path based on different situations.
And once you understand decision making, you're ready for one of the most important concepts in programming:

0 Comments