If Statements


The if statement is a fundamental control flow structure in JavaScript that executes a block of code only when a specified condition evaluates to true. This allows for decision-making within your programs, enabling different outcomes based on varying inputs or states.


Example 1: Basic if Statement

// This example checks if a number is positive.
let number = 10;

if (number > 0) {
  console.log("The number is positive."); // This code runs because the condition is true.
}

Explanation

The code checks if the value of the number variable is greater than 0. Since 10 is greater than 0, the condition is true, and the message is logged to the console.


Example 2: Using the Equality Operator

// This example checks if a user is an admin.
let userRole = 'admin';

if (userRole === 'admin') {
  console.log("Welcome, Admin!"); // This code executes because userRole is 'admin'.
}

Explanation

This example uses the strict equality operator (===) to compare the userRole variable to the string 'admin'. The condition is met, so the welcome message is displayed.


Example 3: Checking for a Boolean Value

// This example checks if a user is logged in.
let isLoggedIn = true;

if (isLoggedIn) {
  console.log("User is currently logged in."); // This runs because isLoggedIn is true.
}

Explanation

Here, the if statement directly checks the boolean value of isLoggedIn. Because isLoggedIn is true, the code block is executed.


Example 4: Condition with a Logical AND (&&)

// This example checks if a person is eligible to vote.
let age = 25;
let isCitizen = true;

if (age >= 18 && isCitizen) {
  console.log("You are eligible to vote."); // Executes if both conditions are true.
}

Explanation

This code uses the logical AND (&&) operator to check two conditions. The message is printed only because age is 18 or greater and isCitizen is true.


Example 5: Condition with a Logical OR (||)

// This example checks if a user has a discount.
let hasCoupon = false;
let isMember = true;

if (hasCoupon || isMember) {
  console.log("A discount has been applied."); // Executes if at least one condition is true.
}

Explanation

The logical OR (||) operator in this if statement checks if either hasCoupon is true or isMember is true. Since isMember is true, the condition is met and the message is logged.


Example 6: Using the Not Equal Operator

// This example checks if a traffic light is red.
let trafficLight = 'green';

if (trafficLight !== 'red') {
  console.log("It is safe to proceed."); // This runs because the light is not red.
}

Explanation

This example utilizes the strict not equal operator (!==) to determine if the trafficLight variable is not 'red'. The condition evaluates to true because 'green' is not 'red'.


Example 7: Checking for an Undefined Variable

// This example checks if a variable has been assigned a value.
let user; // The variable is declared but not initialized.

if (user === undefined) {
  console.log("Please enter your name."); // Executes because user is undefined.
}

Explanation

The code checks if the user variable has the value undefined. Since user was declared but not assigned a value, the condition is true, and the prompt is displayed.