JavaScript Conditions

JavaScript if Statement

The JavaScript if statement is used to run a block of code only when a condition is true.

It helps JavaScript make decisions.

Example

Example

javascript

let age = 20;

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

In this example, the code inside the if block runs only when age is greater than or equal to 18.

What is an if Statement?

An if statement checks a condition.

If the condition is true, JavaScript runs the code inside the curly braces {}.

If the condition is false, JavaScript skips that code.

Syntax

Syntax

javascript

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

The condition is written inside parentheses ().

The code block is written inside curly braces {}.

Simple if Statement Example

Example

javascript

let marks = 75;

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

Here, the condition is:

Condition

javascript

marks >= 40

Since marks is greater than 40, the message will run.

if Statement with Numbers

You can use an if statement to compare numbers.

Example

javascript

let price = 1200;

if (price > 1000) {
  console.log("This product is expensive.");
}

Here, the message runs only if price is greater than 1000.

if Statement with Strings

You can also compare string values.

Example

javascript

let userName = "John";

if (userName === "John") {
  console.log("Hello John!");
}

Here, the condition checks whether userName is exactly equal to "John".

if Statement with Boolean Values

Boolean values are commonly used with if statements.

Example

javascript

let isLoggedIn = true;

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

This is the same as writing:

Example

javascript

if (isLoggedIn === true) {
  console.log("Welcome back!");
}

The shorter version is cleaner and commonly used.

Using Comparison Operators with if

Comparison operators are often used inside if conditions.

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

Example

javascript

let score = 90;

if (score >= 80) {
  console.log("Excellent score.");
}

Using Logical Operators with if

Logical operators are used when you want to check more than one condition.

Example

javascript

let age = 22;
let hasId = true;

if (age >= 18 && hasId) {
  console.log("You can enter.");
}

Here, both conditions must be true:

ConditionMeaning
age >= 18Age must be 18 or above
hasIdUser must have an ID

The && operator means AND.

Complete Example

Complete Example

html

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

  <h1>JavaScript if Statement</h1>

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

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

    if (age >= 18) {
      message = userName + " is 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 = 16;

Then check whether the message appears or not.

Important Points

  • The if statement is used to run code only when a condition is true.
  • The condition is written inside parentheses ().
  • The code block is written inside curly braces {}.
  • If the condition is false, the code inside the if block is skipped.
  • Comparison operators are commonly used with if statements.
  • Logical operators can be used to check multiple conditions.
  • Use === for strict comparison in JavaScript.

Conclusion

The JavaScript if statement is used to make decisions in code.

It checks a condition and runs a block of code only when that condition is true.

The if statement is one of the most important parts of JavaScript because it helps create dynamic and interactive programs.