Conditional Statements in JavaScript
Yesterday, I learned how JavaScript uses operators to perform calculations and compare values.
Today, I explored Conditional Statements, which allow a program to make decisions based on different conditions.
Just like humans make decisions every day, JavaScript also needs a way to choose what code should run depending on a situation.
Let's dive in.
What are Conditional Statements?
Conditional statements allow JavaScript to execute different blocks of code depending on whether a condition is true or false.
Example:
let age = 20;
if (age >= 18) {
console.log("You can vote.");
}
Since the condition is true, the message is displayed.
Why Do We Need Conditional Statements?
Imagine building:
Login Systems
ATM Machines
Online Shopping Websites
Weather Apps
Games
All of them need to make decisions.
For example:
Is the user logged in?
Is the password correct?
Is the product in stock?
Is the score greater than 100?
Conditional statements solve these problems.
The if Statement
The simplest conditional statement.
let temperature = 32;
if (temperature > 30) {
console.log("It's a hot day!");
}
If the condition is true, the code inside the block executes.
The if...else Statement
Runs one block if the condition is true and another if it's false.
let isLoggedIn = false;
if (isLoggedIn) {
console.log("Welcome back!");
} else {
console.log("Please log in.");
}
Only one block executes.
The if...else if...else Statement
Used when there are multiple possible conditions.
let marks = 82;
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("Fail");
}
JavaScript checks each condition from top to bottom.
The first true condition is executed.
Nested if Statements
An if statement can be placed inside another if.
let age = 22;
let hasLicense = true;
if (age >= 18) {
if (hasLicense) {
console.log("You can drive.");
}
}
Nested conditions are useful when multiple requirements must be satisfied.
The switch Statement
Used when checking a single value against many possible cases.
Example:
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid Day");
}
The break statement prevents execution from continuing into the next case.
if...else vs switch
| if...else | switch |
|---|---|
| Good for ranges | Good for exact values |
| More flexible | Cleaner for many fixed options |
| Complex conditions | Single variable comparisons |
Choose the one that best fits the problem.
Truthy & Falsy Values
JavaScript automatically treats some values as true or false.
Falsy values:
false0""(empty string)nullundefinedNaN
Everything else is generally considered truthy.
Example:
let username = "";
if (username) {
console.log("User Found");
}
else {
console.log("No Username");
}
Output:
No Username
Best Practices
✔ Write simple and readable conditions.
✔ Avoid deeply nested if statements.
✔ Use switch when checking one variable against many fixed values.
✔ Use strict comparison (===) whenever possible.
✔ Keep your conditions easy to understand.
My Biggest Takeaway
Today, I learned that programming is all about making decisions.
Conditional statements allow JavaScript to respond differently depending on different situations.
Whether it's validating a login, checking grades, or controlling game logic, decision-making is at the heart of programming.
Understanding if, else, else if, and switch has given me the tools to write programs that can react intelligently instead of following a single fixed path.
