🎭 Using If, Else, and Else If in JavaScript

JavaScript Guide

The Power of Conditional Statements (The Brain, Conscience, and Decision-Maker of Code)

JavaScript is a robot 🤖
But if you don’t tell it what to do, it just stares at the screen blankly.

That’s where if, else, and else if come in
→ they are JavaScript’s decision-making mechanism: when to do what.


🧠 WHAT IS IF?

“IF THIS IS TRUE, DO THIS”

The most basic building block.
You’re basically telling JavaScript:

“Hey buddy, if this condition is true, go inside.
If not… act like it never existed.”

🔹 The Simplest Form

if (age >= 18) {
  console.log("You can get a driver’s license 🚗");
}

📌 What’s happening here?

  • age >= 18 → the condition
  • If the condition is true → the code inside { } runs
  • If it’s false → nothing happens (JavaScript ghost mode 👻)

👉 In short:

  • 18 and over → message appears
  • Under 18 → silence… disappointment…

😂 Real-Life Analogy

if (coffeeExists) {
  console.log("I can write code ☕💻");
}

No coffee?

  • No code
  • No motivation
  • No life

⚠️ GOLDEN RULES WHEN USING IF

❌ The Biggest Mistake: Single equals sign

if (x = 5) {

Do you know what this does?

  • Assigns 5 to x
  • 5 is considered true
  • The if block runs every single time

😱 Which basically means:

if (true) {

✅ The Correct Way:

if (x === 5) {

💡 Remember:

  • = → assignment
  • == → loose comparison
  • === → king-level comparison 👑

🙃 WHAT IS ELSE?

“IF THAT DOESN’T HAPPEN, AT LEAST DO THIS”

else is the grumpy sibling of if.
If if doesn’t run, else takes the stage.

🔹 Basic Usage

if (weather === "rainy") {
  console.log("Take your umbrella ☔");
} else {
  console.log("Put on your sunglasses 😎");
}

📌 The flow:

  • JavaScript checks → is it rainy?
  • Yes → if
  • No → no escape, else

If else exists, one of them will always run.


😂 Developer Psychology

if (projectFinished) {
  console.log("Going on vacation 🏖️");
} else {
  console.log("Let me push one more commit 😭");
}

Spoiler:
else usually runs.


🧩 WHAT IS ELSE IF?

“WAIT… LET’S CHECK ONE MORE THING”

Life isn’t binary.
Neither is JavaScript.

🔹 Multi-Option Scenario

if (score >= 90) {
  console.log("Excellent 🏆");
} else if (score >= 70) {
  console.log("Good 👍");
} else if (score >= 50) {
  console.log("Pass 😐");
} else {
  console.log("See you next year 😬");
}

📌 How does JavaScript think?

  • It checks from top to bottom ⬇️
  • Stops at the first true condition
  • Completely ignores the rest

⚠️ Order is CRITICAL
90 must be at the top, otherwise everyone becomes “good” 😅


🧠 GOLDEN TIPS FOR ELSE IF CHAINS

❌ Bad Example

if (score >= 50) {
  console.log("Passed");
} else if (score >= 90) {
  console.log("Excellent");
}

What happens to someone who scored 90?
👉 First condition runs
👉 “Excellent” becomes a dream

✅ Correct Order

if (score >= 90) {
  console.log("Excellent");
} else if (score >= 50) {
  console.log("Passed");
}


🎯 BOOLEAN LOGIC: WHAT DOES IF LOVE?

if loves booleans ❤️
That means anything that evaluates to true / false.

✔️ These are fine:

if (x > 10)
if (isLoggedIn)
if (userName)

❌ These are risky:

if ("")        // false
if (0)         // false
if (null)      // false
if (undefined) // false

💡 JavaScript has a hidden world called “truthy / falsy” 🌍


🤹‍♂️ PRACTICAL & FUN EXAMPLES

🎮 Game Life Check

if (life > 0) {
  console.log("Game continues 🎮");
} else {
  console.log("Game Over 💀");
}

Explanation:

  • Life exists → hope exists
  • No life → tombstone

🔐 Login Check

if (username && password) {
  console.log("Login successful ✅");
} else {
  console.log("Fill in all fields 🙄");
}

Explanation:

  • Both filled → login
  • Even one missing → scolding

🚀 HOW TO WRITE CLEANER IF–ELSE

❌ Messy

if (isAdmin === true) {
  console.log("You have permission");
}

✅ Clean

if (isAdmin) {
  console.log("You have permission");
}

JavaScript says:

“You don’t need to say ‘true’ again.”


🧨 WHEN NOT TO USE IF

  • Too many else if blocks 😵
  • Hard to read
  • Your brain starts melting 🔥

👉 Then consider:

  • switch
  • ternary
  • or splitting logic into functions

🏁 CONCLUSION:

THOSE WHO KNOW IF–ELSE WRITE CODE,

THOSE WHO UNDERSTAND IT BUILD SOFTWARE

if, else, else if:

  • Are the decision-making core of your code 🧠
  • Shape user experience 🎯
  • Help you move from junior to mid-level 🚀

A well-written if block means:

  • Fewer bugs
  • Cleaner code
  • Fewer “why isn’t this working?” moments 😄

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir