JavaScript Tutorial – Day 5: If-Else Statements in JavaScript
3 mins read

JavaScript Tutorial – Day 5: If-Else Statements in JavaScript

One of the most powerful features in any programming language is the ability to make decisions. In this JavaScript tutorial, we’ll explore if-else statements in JavaScript. By the end of this lesson, you’ll be able to control the flow of your code based on conditions—just like how humans make choices in daily life.


What are If-Else Statements in JavaScript?

An if-else statement is a way for your program to make decisions. It checks a condition (something that can be true or false) and then decides which block of code to run based on that.

Think of it like a fork in the road:

  • If the condition is false → go the other way
  • If the condition is true → go one way

👉 Example:

let age = 18;

if (age >= 18) {
  console.log("You can vote!");
} else {
  console.log("You are too young to vote.");
}

Here, the program checks if age >= 18.

  • If true → prints "You can vote!".
  • If false → prints "You are too young to vote.".

Syntax of If-Else Statement

if (condition) {
  // code to run if condition is true
} else {
  // code to run if condition is false
}

If-Else If Ladder in JavaScript

Sometimes you need to check multiple conditions. That’s where the else if ladder is used.

👉 Example:

let marks = 85;

if (marks >= 90) {
  console.log("Grade: A+");
} else if (marks >= 75) {
  console.log("Grade: A");
} else if (marks >= 50) {
  console.log("Grade: B");
} else {
  console.log("Grade: F");
}

This checks conditions one by one and executes the first true block.


Nested If-Else Statements in JavaScript

You can also use if-else inside another if-else. This is called nested if-else.

👉 Example:

let username = "Anand";
let password = "12345";

if (username === "Anand") {
  if (password === "12345") {
    console.log("Login successful");
  } else {
    console.log("Incorrect password");
  }
} else {
  console.log("User not found");
}

Real-Life Example: Online Shopping Delivery

let cartValue = 600;
let isMember = true;

if (cartValue > 1000) {
  console.log("Free delivery!");
} else if (isMember) {
  console.log("Delivery charge waived for members.");
} else {
  console.log("Delivery charge: ₹50");
}

👉 This combines comparison operators with if-else statements.


Key Takeaways

  • Control the flow of logic.
  • Allow decision-making in programs.
  • Used in login systems, grading, e-commerce, and games.
  • Forms the base for more advanced concepts like loops and functions.

External Resource

👉 Learn more at the official MDN JavaScript if…else guide.


What’s Next?

In this JavaScript tutorial series:

  • ✅ Day 1 → First JavaScript Code
  • ✅ Day 2 → Variables in JavaScript
  • ✅ Day 3 → Data Types in JavaScript
  • ✅ Day 4 → Operators in JavaScript
  • ✅ Day 5 → If-Else Statements in JavaScript
  • 🔜 Day 6 → Switch Case in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *