Skip to content

JavaScript if else

This lesson explains JavaScript if else in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.

JavaScript if...else Overview

The JavaScript if...else statement is used to execute different blocks of code based on whether a condition evaluates to true or false. Conditional statements allow applications to make decisions, validate user input, control program flow, and implement business logic.

JavaScript supports simple if statements, if...else, multiple else if conditions, and nested conditional statements. These are among the most frequently used control structures in modern JavaScript development.

Statement Purpose Typical Usage
if Execute code when a condition is true. Single condition checks.
if...else Choose between two code paths. Login validation, age verification.
else if Evaluate multiple conditions. Grading systems, user roles.
Nested if Evaluate conditions inside another condition. Complex business logic.

JavaScript if...else Example

const age = 20;

if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

This example checks whether the value of age is greater than or equal to 18. If the condition is true, the first block executes; otherwise, the code inside the else block runs.

When to Use if...else Statements

Scenario Recommended Statement
Single condition if
Two possible outcomes if...else
Multiple conditions if...else if...else
Complex decision making Nested if statements
Simple value selection Ternary operator

Top 10 Useful if...else Examples

# Use Case Condition Example Console Output
1 Age Verification age >= 18 Adult or Minor
2 Login Check isLoggedIn === true Welcome back or Please log in
3 Password Length password.length >= 8 Strong password or Password too short
4 Even or Odd Number num % 2 === 0 Even or Odd
5 Discount Eligibility cartTotal >= 1000 Discount applied or No discount
6 Input Validation email.includes("@") Valid email or Invalid email
7 Role-Based Access role === "admin" Access granted or Access denied
8 Temperature Alert temperature > 30 Hot weather or Normal weather
9 Stock Availability stock > 0 In stock or Out of stock
10 Marks Result marks >= 35 Pass or Fail

Best Practices

  • Keep conditional statements simple and easy to read.
  • Use meaningful variable names in conditions.
  • Prefer strict equality using ===.
  • Avoid deeply nested if statements.
  • Group related conditions logically.
  • Use the ternary operator only for simple decisions.
  • Extract complex conditions into helper functions.
  • Always test both true and false conditions.

Common Mistakes

  • Using = instead of comparison operators.
  • Using == instead of ===.
  • Creating unnecessary nested if statements.
  • Writing overly complex conditional expressions.
  • Forgetting the final else block when needed.
  • Ignoring operator precedence in conditions.
  • Not validating input values before comparison.

Key Takeaways

  • The if statement executes code only when a condition is true.
  • if...else provides two execution paths.
  • else if allows multiple conditions to be evaluated.
  • Use strict comparison operators for more predictable results.
  • Readable conditional logic makes applications easier to maintain.
  • Conditional statements are essential for user input validation and business logic.

Pro Tip

If an if...else statement becomes difficult to read, consider simplifying the condition, extracting reusable functions, or using a switch statement when comparing multiple fixed values.