JavaScript Conditionals: If, Else, and Else If Explained Like You're a Human

Tired of robotic tutorials? Let's talk about JavaScript if, else, and else if statements in plain English. Learn how to make your code make decisions, just like you do every day.

JavaScript Conditionals: If, Else, and Else If Explained Like You're a Human
JavaScript If, Else, and Else If: Teaching Your Code to Make Decisions
Let’s be honest. A lot of coding tutorials can feel like they’re written for robots, by robots. You’re left staring at a screen full of if (x === y) { }
and your eyes just glaze over.
But what if I told you that you use the logic behind if
, else
, and else if
every single day? You absolutely do. Programming is just about teaching your computer to do the same.
Let's break this down like we're having a coffee chat.
The "If" Statement: Your Daily Decision Maker
Think about your morning. You might say to yourself: "If it is raining, then I will take an umbrella."
This is a conditional statement! You're making a decision based on a condition (the weather).
In JavaScript, we write this exact thought like so:
javascript
let isRaining = true;
if (isRaining === true) {
console.log("Grab your umbrella! ☔");
}
// Output: "Grab your umbrella! ☔"
We set a variable (isRaining
) to hold our condition. The if
keyword checks if that condition is true
. If it is, it runs the code inside the curly braces { }
. It’s that simple.
We can even write it shorter. Since isRaining
is already true
, we can just use the variable itself in the condition:
javascript
if (isRaining) {
console.log("Grab your umbrella! ☔");
}
The "Else" Statement: Your Plan B
Now, what if it's not raining? Your original plan was just to head out the door. In human terms: "If it is raining, I'll take an umbrella. Otherwise, I'll just leave."
That "Otherwise" is your else
clause.
javascript
let isRaining = false;
if (isRaining) {
console.log("Grab your umbrella! ☔");
} else {
console.log("Enjoy the sunshine! 😎");
}
// Output: "Enjoy the sunshine! 😎"
The else
block is a catch-all. It only runs if the if
condition is false
. It’s your code's backup plan.
The "Else If" Statement: For Those "Well, Actually..." Moments
Life is rarely a simple yes/no, either/or. It's full of nuances. What if it's not raining, but it's super windy? Or super sunny? You need more options!
This is where else if
comes in. It lets you chain multiple conditions together.
Let's expand our weather plan:
If it is raining → take an umbrella.
Else, if it is sunny → wear sunglasses.
Else, if it is snowy → wear a coat.
Else (for everything else) → just bring a jacket, you can't be sure.
javascript
let weather = "sunny";
if (weather === "raining") {
console.log("Grab your umbrella! ☔");
} else if (weather === "sunny") {
console.log("Wear those sunglasses! 😎");
} else if (weather === "snowing") {
console.log("Bundle up, it's cold! ⛄");
} else {
console.log("Better bring a jacket, just in case.");
}
// Output: "Wear those sunglasses! 😎"
The computer will check each condition in order. The moment it finds one that is true
, it runs that block of code and ignores all the other else if
and else
statements below it.