Conditional Statements in JavaScript
Conditional statements allow your program to make decisions. They help your code choose between different actions based on certain conditions. This is one of the most important topics for beginners because almost every real-world program employs conditional statements.
In simple words:
Conditions help your program answer questions like
"Should I do this or something else?"
In this chapter, you will learn:
if
else
else if
nested conditions
These will make your code smarter and responsive.
Why Do We Need Conditions?
Conditions are used when you want your program to take different actions depending on the situation.
Examples in real life:
If marks = 35 ? Pass
If age = 18 ? Can vote
If password matches ? Login
If cart is empty ? Show message
You will use conditions in almost every project.
if Statement
The if statement runs code only when the condition is true.
Syntax:
if (condition) {
// code to run if condition is true
}
Example:
let age = 20;
if (age >= 18) {
console.log("You are an adult");
}
Output:
You are an adult
else Statement
The else block runs when the condition is false.
Example:
let age = 15;
if (age >= 18) {
console.log("You can drive");
} else {
console.log("You cannot drive");
}
Output:
You cannot drive
else if Statement
Use else if when you have multiple conditions.
Example:
let marks = 72;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 75) {
console.log("Grade B");
} else if (marks >= 50) {
console.log("Grade C");
} else {
console.log("Failed");
}
Output:
Grade B
The program checks conditions from top to bottom until one matches.
Nested if (if inside if)
Sometimes you need to check one condition inside another.
Example:
let age = 22;
let hasID = true;
if (age >= 18) {
if (hasID) {
console.log("Entry allowed");
} else {
console.log("ID required");
}
} else {
console.log("Underage");
}
Output:
Entry allowed
Nested conditions are powerful but should be used judiciously to maintain readability.
Using Conditions with Logical Operators
You can combine multiple conditions in a single line.
Example:
let age = 18;
let hasLicense = true;
if (age >= 18 && hasLicense) {
console.log("You can drive");
} else {
console.log("You cannot drive");
}
Output:
You can drive
Real-Life Example Program
Let us create a small program to check whether a student passed or failed and provide a message based on marks.
let marks = 33;
if (marks >= 80) {
console.log("Excellent! You got distinction.");
} else if (marks >= 60) {
console.log("Good job! You passed with first class.");
} else if (marks >= 35) {
console.log("You passed.");
} else {
console.log("You failed.");
}
Output:
You passed.
This example illustrates how conditions facilitate clear decision-making in programs.
More Useful Examples for Practice
Check if a number is positive or negative
let num = -5;
if (num >= 0) {
console.log("Positive");
} else {
console.log("Negative");
}
Check if a number is even or odd
let number = 12;
if (number % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
Common Mistakes Beginners Make
Forgetting curly braces
{}Confusing
=with==or===Writing conditions in the wrong order
Using too many nested if statements
Not formatting code properly
Avoid these mistakes to write clean and understandable code.
Practice Tasks (Do It Yourself)
Check if the entered age is eligible for voting.
Write a program to find the largest of three numbers.
Print grade based on marks (A, B, C, Fail).
Check if a username and password match the stored values.
Create a program that checks if a number is divisible by both 3 and 5.