JavaScript Conditions

JavaScript if else Statement

The JavaScript if else statement is used when you want to run one block of code if a condition is true, and another block of code if the condition is false.

Example

Example

javascript

let age = 16;

if (age >= 18) {
  console.log("You are eligible.");
} else {
  console.log("You are not eligible.");
}

In this example, if age is 18 or more, the first message runs. Otherwise, the second message runs.

What is an if else Statement?

The if else statement gives JavaScript two choices.

If the condition is true, the if block runs.

If the condition is false, the else block runs.

Syntax

Syntax

javascript

if (condition) {
  // code runs when condition is true
} else {
  // code runs when condition is false
}

Simple if else Example

Example

javascript

let marks = 35;

if (marks >= 40) {
  console.log("You passed the exam.");
} else {
  console.log("You failed the exam.");
}

Here, JavaScript checks whether marks is greater than or equal to 40.

If the condition is true, it shows the pass message.

If the condition is false, it shows the fail message.

if else with Numbers

You can use if else to compare numbers.

Example

javascript

let temperature = 35;

if (temperature > 30) {
  console.log("It is hot today.");
} else {
  console.log("The weather is normal.");
}

Here, the message depends on the value of temperature.

if else with Strings

You can also use if else with string values.

Example

javascript

let userName = "Emma";

if (userName === "Emma") {
  console.log("Welcome Emma!");
} else {
  console.log("Unknown user.");
}

Here, JavaScript checks whether userName is exactly equal to "Emma".

if else with Boolean Values

Boolean values are commonly used with if else.

Example

javascript

let isLoggedIn = false;

if (isLoggedIn) {
  console.log("Welcome back!");
} else {
  console.log("Please log in.");
}

If isLoggedIn is true, the first message runs.

If it is false, the second message runs.

Using Comparison Operators

Comparison operators are often used inside if else conditions.

Example

javascript

let price = 900;

if (price > 1000) {
  console.log("Expensive product.");
} else {
  console.log("Affordable product.");
}

Common comparison operators:

OperatorMeaning
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
===Equal value and equal type
!==Not equal value or not equal type

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript if else Statement</title>
</head>
<body>

  <h1>JavaScript if else Statement</h1>

  <p id="result"></p>

  <script>
    let userName = "John";
    let age = 16;
    let message = "";

    if (age >= 18) {
      message = userName + " is eligible to vote.";
    } else {
      message = userName + " is not eligible to vote.";
    }

    document.getElementById("result").innerHTML = message;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing the value of age.

Example

javascript

let age = 22;

Then check how the message changes.

Important Points

  • The if else statement is used to handle two possible results.
  • The if block runs when the condition is true.
  • The else block runs when the condition is false.
  • The else block does not have a condition.
  • Use if else when you want JavaScript to choose between two actions.
  • Comparison operators are commonly used inside if else conditions.

Conclusion

The JavaScript if else statement is used to make decisions with two possible outcomes.

It runs one block of code when the condition is true and another block when the condition is false.

This makes JavaScript programs more dynamic and useful for real-world situations.