JavaScript Conditional Statements – if, else, switch

Himmat Kumar Mar 29, 2025, 11:02 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.

Comments

Please login to leave a comment.

No comments yet.

Related Posts

javascript-functions-and-scope-guide
Himmat Kumar Mar 30, 2025, 5:26 AM

JavaScript Functions & Scope

javascript-date-and-time-guide
Himmat Kumar Mar 30, 2025, 6:46 AM

Date and Time in JavaScript — Easy Guide

javascript-variables-var-let-const-explained
Himmat Kumar Mar 25, 2025, 12:03 PM

JavaScript Variables: var vs let vs const

javascript-data-types-explained
Himmat Kumar Mar 25, 2025, 12:17 PM

JavaScript Data Types Explained with Examples

javascript-loops-for-while-do-while
Himmat Kumar Mar 29, 2025, 5:43 AM

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

introduction-to-javascript
Himmat Kumar Oct 27, 2023, 11:36 AM

Introduction JavaScript

gk-questions-answers-2024
502 viewsGK
Himmat Kumar Aug 20, 2024, 4:10 AM

100 Latest General Knowledge Questions and Answers (202...