JAVASCRIPT

Else if in JavaScript

Using if and else statements, we can write a program that shows one greeting if hour < 12 is true and another if it's false.

Syntax of Else If

The basic syntax of an if / else if / else statement is:

let hour = 9;
if (hour < 12) {
  console.log("Good morning");
} else {
  console.log("Good night");
}

For a more specific condition, like if hour is not less than 12 but is less than 17, we can code else if (hour < 17) instead.

let hour = 14;
if (hour < 12) {
  console.log("Good morning");
} else if (hour < 17) {
  console.log("Good afternoon");
} else {
  console.log("Good evening");
}

Here, the first condition checks if hour is less than 12. If it's not, JavaScript moves to the else block, where it evaluates the specified condition in else if. If that condition is also false, it moves to the else statement.

Else If and Comparison Operators

Else if statements frequently use comparison operators like <, >, <=, and >= to define precise conditions.

const age = 20;
if (age < 13) {
  console.log("Child");
} else if (age < 18) {
  console.log("Teenager");
} else {
  console.log("Adult");
}

Using Else If in Web Development

In web development, JavaScript's else if statements help create interactive applications. When combined with web APIs, they can dynamically adjust content based on user input or external data.

For instance, modifying an HTML element based on user input:

const time = new Date().getHours();
if (time < 12) {
  document.body.style.backgroundColor = "lightyellow";
} else if (time < 18) {
  document.body.style.backgroundColor = "lightblue";
} else {
  document.body.style.backgroundColor = "darkblue";
}

Else If in Other Programming Languages

Similar structures exist in other programming languages like Java and Python.

Else If in Java

int num = 10;
if (num < 5) {
  System.out.println("Small number");
} else if (num < 10) {
  System.out.println("Medium number");
} else {
  System.out.println("Large number");
}

Else If in Python

num = 10
if num < 5:
    print("Small number")
elif num < 10:
    print("Medium number")
else:
    print("Large number")

Using Else If with AJAX and Async Functions

Modern web applications use AJAX and async operations to handle asynchronous data. else if structures help manage responses dynamically:

async function fetchData() {
  let response = await fetch("https://api.example.com/data");
  if (response.status === 200) {
    console.log("Data received");
  } else if (response.status === 404) {
    console.log("Not found");
  } else {
    console.log("Server error");
  }
}

Else If and CSS Interactions

In CSS-driven animations and styles, JavaScript can modify styles dynamically using else if statements:

let theme = "dark";
if (theme === "light") {
  document.body.style.backgroundColor = "white";
} else if (theme === "dark") {
  document.body.style.backgroundColor = "black";
} else {
  document.body.style.backgroundColor = "gray";
}

Else If vs Switch Statement

A switch statement is an alternative to else if when multiple conditions depend on the same variable.

let fruit = "apple";
switch (fruit) {
  case "banana":
    console.log("Banana selected");
    break;
  case "apple":
    console.log("Apple selected");
    break;
  default:
    console.log("Unknown fruit");
}

Else If and Curly Braces

Curly braces {} are essential in JavaScript for defining the block of code in if, else if, and else statements. Omitting them can cause unintended behavior:

if (true) console.log("Hello");
else if (false) console.log("Not possible");
else console.log("Fallback");

When multiple statements exist, curly braces ensure all grouped statements execute correctly:

if (true) {
  console.log("Hello");
  console.log("World");
} else {
  console.log("Goodbye");
}

Else If and Block of Code

A block of code inside an if, else if, or else statement consists of multiple statements enclosed in curly braces:

if (true) {
  let message = "Inside block";
  console.log(message);
}

Each block of code allows us to do something specific when conditions are met.

The else if statement refines conditional statements, providing multiple evaluation paths in JavaScript. Whether used in web development, function calls, node.js, iteration, or handling NaN values, mastering else if improves logical flow and decision-making. It complements switch statements, comparison operators, AJAX, async functions, and ternary operators, ensuring precise, readable, and efficient code structures.

Learn JavaScript for Free
Start learning now
button icon
To advance beyond this tutorial and learn JavaScript by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH