codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

9 / 65 topics
9JavaScript if...else Statement10JavaScript switch...case Statement11JavaScript for Loop12JavaScript while and do...while Loop13JavaScript break and continue
Tutorials/JavaScript/JavaScript if...else Statement
🌐JavaScript

JavaScript if...else Statement

Updated 2026-04-20
3 min read

JavaScript if...else Statement

Introduction

The if...else statement is a fundamental control flow structure in JavaScript that allows you to execute different blocks of code based on specified conditions. It enables your programs to make decisions and respond accordingly, making it essential for creating dynamic and interactive web applications.

In this tutorial, we will explore the syntax, usage, best practices, and real-world examples of the if...else statement in JavaScript.

Basic Syntax

The basic syntax of an if...else statement is as follows:

if (condition) {
  // Code to execute if condition is true
} else {
  // Code to execute if condition is false
}
  • Condition: This is an expression that evaluates to a boolean value (true or false). If the condition is true, the code inside the if block will be executed. If it's false, the code inside the else block will be executed.

Example

Let's consider a simple example where we check if a number is positive, negative, or zero:

let number = 5;

if (number > 0) {
  console.log("The number is positive.");
} else if (number < 0) {
  console.log("The number is negative.");
} else {
  console.log("The number is zero.");
}

In this example:

  • The if block checks if the number is greater than zero.
  • The else if block checks if the number is less than zero.
  • The else block executes if none of the above conditions are true, meaning the number is zero.

Multiple Conditions with else if

You can chain multiple else if statements to handle more complex scenarios. Here's an example that categorizes a user based on their age:

let age = 25;

if (age < 18) {
  console.log("You are a minor.");
} else if (age >= 18 && age < 65) {
  console.log("You are an adult.");
} else {
  console.log("You are a senior citizen.");
}

In this example:

  • The first if block checks if the user is under 18.
  • The second else if block checks if the user is between 18 and 64 (inclusive).
  • The final else block handles users who are 65 or older.

Best Practices

1. Use Parentheses for Clarity

Always use parentheses around conditions to improve readability, especially when dealing with complex expressions:

if ((age >= 18 && age < 30) || (age > 60)) {
  console.log("You are in a specific age group.");
}

2. Avoid Deep Nesting

Deeply nested if...else statements can make your code difficult to read and maintain. Consider using alternative structures like switch statements or restructuring your logic:

// Bad practice
if (condition1) {
  if (condition2) {
    // Code block
  }
}

// Better approach
if (condition1 && condition2) {
  // Code block
}

3. Use Early Returns

Using early returns can simplify your code by reducing the nesting level:

function checkUserStatus(age) {
  if (age < 0) {
    return "Invalid age";
  }
  
  if (age < 18) {
    return "Minor";
  } else if (age >= 18 && age < 65) {
    return "Adult";
  } else {
    return "Senior citizen";
  }
}

4. Handle Edge Cases

Always consider edge cases and provide appropriate handling:

let input = null;

if (input === null || input === undefined) {
  console.log("Input is not provided.");
} else {
  console.log("Input is valid.");
}

Real-World Examples

Example 1: Login System

Here's a simple login system that checks user credentials:

let username = "admin";
let password = "password123";

if (username === "admin" && password === "password123") {
  console.log("Login successful.");
} else {
  console.log("Invalid credentials.");
}

Example 2: Discount Calculation

A shopping cart system that applies a discount based on the total purchase amount:

let totalAmount = 500;

if (totalAmount >= 1000) {
  console.log("You get a 10% discount!");
} else if (totalAmount >= 500) {
  console.log("You get a 5% discount!");
} else {
  console.log("No discount available.");
}

Example 3: Weather App

A weather app that provides different messages based on the temperature:

let temperature = 75;

if (temperature < 32) {
  console.log("It's freezing outside!");
} else if (temperature >= 32 && temperature < 60) {
  console.log("It's chilly.");
} else if (temperature >= 60 && temperature < 80) {
  console.log("It's warm.");
} else {
  console.log("It's hot!");
}

Conclusion

The if...else statement is a powerful tool in JavaScript for controlling the flow of your programs based on conditions. By understanding its syntax, best practices, and real-world applications, you can write more efficient and maintainable code.

Remember to always keep your conditions clear, avoid deep nesting, handle edge cases, and use early returns where appropriate. With these guidelines, you'll be well-equipped to implement conditional logic in your JavaScript projects effectively.


PreviousJavaScript Type ConversionNext JavaScript switch...case Statement

Recommended Gear

JavaScript Type ConversionJavaScript switch...case Statement