JavaScript Conditional Statements – if, else, switch

Himmat Kumar Jun 1, 2025, 6:37 AM
Blog Thumbnail

🧠 JavaScript Conditional Statements – Explained Simply

Conditional statements in JavaScript let your code make decisions—just like you do in real life. Think of them as "if this happens, do that" kind of logic. They help your program figure out what to do in different situations.

Here are the most common ones:

1. if Statement

Use an if statement when you want to run some code only if a certain condition is true.

let age = 20;

if (age >= 18) {
  console.log("You are an adult.");
}

🔍 What’s happening? If the age is 18 or more, it’ll say “You are an adult.” If not, it won’t do anything.

2. if...else Statement

Need to do one thing if the condition is true, and something else if it’s false? That’s where else comes in.

let isRaining = true;

if (isRaining) {
  console.log("Take an umbrella!");
} else {
  console.log("Enjoy the sunshine!");
}

🌧️☀️ It’s like checking the weather before heading out.

3. else if Statement

Sometimes, one condition isn’t enough. You might want to check for multiple possibilities. That’s where else if helps.

let score = 75;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
} else if (score >= 70) {
  console.log("Grade: C");
} else {
  console.log("Grade: D");
}

🎓 It’s a grading system: the better the score, the better the grade.

4. switch Statement

When you're checking one specific value against multiple options, switch is your go-to. It’s cleaner than stacking a bunch of if...else statements.

let day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start of the week!");
    break;
  case "Friday":
    console.log("Almost weekend!");
    break;
  case "Sunday":
    console.log("Relax day.");
    break;
  default:
    console.log("Just another day.");
}

📆 Think of it as a weekday menu—what happens depends on the day you pick.

✅ Quick Recap

  • if – Runs only if the condition is true.
  • else – Runs if the condition is false.
  • else if – Checks extra conditions in order.
  • switch – Perfect when you're checking one value against many options.
💡 Pro Tip: Use switch when you're working with the same variable, and stick with if-else when your conditions are more diverse or complex.

Related Posts

Himmat Kumar Jun 1, 2025, 1:20 AM

JavaScript Data Types Explained with Examples

Himmat Kumar Jun 1, 2025, 1:15 AM

JavaScript Variables: var vs let vs const

Himmat Kumar Jun 1, 2025, 1:11 AM

JavaScript Functions & Scope

Himmat Kumar Jun 1, 2025, 1:07 AM

Introduction JavaScript

Himmat Kumar Jun 1, 2025, 1:07 AM

JavaScript Loops — for, while, do...while

Himmat Kumar Jun 1, 2025, 1:07 AM

Date and Time in JavaScript — Easy Guide

45 viewsSEO
Himmat Kumar Jun 1, 2025, 1:14 AM

What is seo ?. Why use seo ?. Seo benefits and disadvan...