JavaScript Code


JavaScript If


If Statement

In JavaScript, if statement is used to execute a block of code based on a condition.

Syntax

if (condition) {
  //code
}

where

  • if is keyword.
  • condition is a boolean expression.
  • code is any valid JavaScript code.

If condition is true, then if-block code is run..

JavaScript if statement

Examples

1. In the following program, we check if number in a is even using if-statement.

a = 10;

if (a%2 == 0) {
  console.log("a is even.");
}

2. In the following program, we check if supplies are good (greater than 5) using an if-statement.

supplies = 10;

if (supplies > 5) {
  console.log("Supplies are good.");
}