Else Statements


The else statement in JavaScript provides an alternative block of code to be executed when the condition in an if statement evaluates to false. This allows for creating more complex decision-making logic in your programs, enabling different outcomes based on different conditions.


Example 1: Basic Age Verification

let userAge = 17;

// Checks if the user is 18 or older
if (userAge >= 18) {
  console.log("You are eligible to vote.");
} else {
  // This block runs if the userAge is less than 18
  console.log("You are not old enough to vote yet.");
}

Explanation

This code checks if the userAge is 18 or greater. Since 17 is not greater than or equal to 18, the condition is false, and the code inside the else block is executed, printing a message indicating the user cannot vote.


Example 2: Checking Login Status

let isLoggedIn = false;

// Determines which message to display based on login status
if (isLoggedIn) {
  console.log("Welcome back to your account!");
} else {
  // Executes because isLoggedIn is false
  console.log("Please log in to access your account.");
}

Explanation

Here, the isLoggedIn variable is false. The if statement checks for a true value, so the condition fails, causing the else block to run and prompt the user to log in.


Example 3: Temperature-Based Clothing Suggestion

let currentTemp = 10; // Temperature in Celsius

// Suggests clothing based on the temperature
if (currentTemp > 20) {
  console.log("It's warm, so a t-shirt should be fine.");
} else {
  // Runs because the temperature is not greater than 20
  console.log("It's a bit chilly; you might want a jacket.");
}

Explanation

This example advises on attire based on currentTemp. Because the temperature, 10, is not greater than 20, the if condition is not met, and the else block's suggestion for a jacket is displayed.


Example 4: Checking for Empty User Input

let usernameInput = "";

// Validates if a username has been entered
if (usernameInput) {
  console.log(`Welcome, ${usernameInput}!`);
} else {
  // Executes as an empty string is a 'falsy' value
  console.log("Username field cannot be empty.");
}

Explanation

In this case, an empty string ("") is a "falsy" value in JavaScript, meaning it behaves like false in a conditional. The if check fails, triggering the else block to handle the empty input.


Example 5: Game Score Comparison

let playerScore = 950;
let highScore = 1000;

// Checks if the player has beaten the high score
if (playerScore > highScore) {
  console.log("Congratulations! You have a new high score!");
} else {
  // This code runs because the player's score is not higher
  console.log("Keep trying to beat the high score!");
}

Explanation

This snippet compares playerScore to highScore. Since 950 is not greater than 1000, the condition is false, and the encouraging message from the else block is shown to the player.


Example 6: Password Length Validation

let userPassword = "pass";

// Enforces a minimum password length
if (userPassword.length >= 8) {
  console.log("Password is a valid length.");
} else {
  // Executes because the password length is less than 8
  console.log("Password must be at least 8 characters long.");
}

Explanation

The code evaluates the length of userPassword. The length is 4, which is not greater than or equal to 8, so the if condition fails and the else block executes to inform the user about the password requirement.


Example 7: Checking for Available Stock

let itemsInStock = 0;

// Checks if an item is available for purchase
if (itemsInStock > 0) {
  console.log("Item is available. You can add it to your cart.");
} else {
  // This runs because there are no items in stock
  console.log("Sorry, this item is currently out of stock.");
}

Explanation

This final example checks itemsInStock. Since the value is 0, the if condition (0 > 0) is false. Consequently, the code inside the else block is executed, notifying the user that the item is unavailable.