JAVASCRIPT

Else in JavaScript

In JavaScript, the else block allows us to define alternative actions when an if statement's specified condition evaluates to false. Instead of creating two separate conditional statements, we use an if / else structure to simplify the logic.

Syntax of if/else

The basic syntax of an if / else statement includes an if statement followed by an else block:

let condition = false;
if (condition) {
  console.log("1");
} else {
  console.log("2");
}

Here, if the condition is truthy, the first block of code executes. If it's falsy (such as false, null, undefined, 0, NaN, or an empty string ""), the else block runs.

Using else with logical operators

Logical operators like && (AND) and || (OR) can be combined within conditional statements:

let x = 5;
if (x > 10 || x === 5) {
  console.log("Condition met");
} else {
  console.log("Condition not met");
}

Using else in Web Development

JavaScript plays a crucial role in web development, often used alongside HTML and CSS. The if / else structure helps modify webpage elements dynamically. For instance, we can change text color based on a user's input:

const userInput = "";
if (userInput) {
  document.body.style.backgroundColor = "lightblue";
} else {
  document.body.style.backgroundColor = "gray";
}

Else in Different Programming Languages

Similar structures exist in other languages like Java and Python:

Else in Java

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

Else in Python

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

Using Else with Switch Statements

An alternative to multiple if / else structures is a switch statement. Unlike if statements, a switch handles multiple conditions more efficiently:

const fruit = "apple";
switch (fruit) {
  case "banana":
    console.log("It's a banana");
    break;
  case "apple":
    console.log("It's an apple");
    break;
  default:
    console.log("Unknown fruit");
}

Else and Ternary Operators

A ternary operator provides a shorthand for if / else:

let score = 80;
let result = score > 70 ? "Pass" : "Fail";
console.log(result);

Debugging and Else Console Statements

When debugging, using console.log inside an else block helps track code execution:

let value = NaN;
if (value) {
  console.log("Truthy value");
} else {
  console.log("Falsy value detected");
}

Additionally, tools like GitHub help track issues while developing complex iteration logic in JavaScript.

Using Else with Functions and Constructors

Functions often use if / else for conditional execution:

function checkNumber(num) {
  if (num > 0) {
    return "Positive";
  } else {
    return "Negative or Zero";
  }
}
console.log(checkNumber(-5));

A function call executes a function at runtime, triggering the appropriate block of code based on the conditions provided.

Object-oriented programming often involves constructors, which can include if / else for initializing object properties:

class User {
  constructor(name) {
    this.name = name || "Guest"; // Default name if falsy
  }
}
const user1 = new User("Alice");
const user2 = new User();
console.log(user1.name); // Alice
console.log(user2.name); // Guest

Else in JSON Parsing

When working with JSON, we often need if / else to handle missing or incorrect data:

const data = "{ \"name\": \"John\" }";
try {
  const parsed = JSON.parse(data);
  console.log(parsed.name);
} catch (error) {
  console.log("Invalid JSON format");
}

Else and Curly Braces

JavaScript requires curly braces {} to define the blocks of code in an if / else statement. These curly braces ensure that multiple statements execute together as a unit:

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

Without curly braces, only the first statement would be part of the condition.

The else block is essential in JavaScript’s conditional statements, providing an alternative execution path when an if statement's condition fails. Whether using it within functions, constructors, switch statements, or ternary operators, mastering else improves code efficiency and readability. Combined with debugging techniques and knowledge of falsy values, it enhances how developers structure their logic in web development.

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