JavaScript if else

Summary: in this tutorial, you will learn how to use the JavaScript if...else statement to execute a block based on a condition.

Introduction to the JavaScript if…else statement

The if statement executes a block if a condition is true. When the condition is false, it does nothing.

But if you want to execute a statement if the condition is false, you can use an if...else statement.

Here’s the syntax of the if...else statement:

if( condition ) {
  // ...
} else { 
  // ...
}Code language: JavaScript (javascript)

In this syntax, the condition is a value or an expression that evaluates to true or false. If the condition is true, the if...else statement executes the block that follows the if branch.

If the condition is false, the if...else statement executes the block that follows the else branch.

Typically, the condition evaluates to a boolean value, which is true or false. However, if it evaluates to a non-boolean value, the if...else statement will convert it to the boolean value.

The following flowchart illustrates how the if...else statement works:

JavaScript if else

JavaScript if…else statement examples

The following example uses the if...else statement to check if the age is greater than or equal to 18:

let age = 18;

if (age >= 18) {
  console.log('You can sign up.');
} else {
  console.log('You must be at least 18 to sign up.');
}Code language: JavaScript (javascript)

In this example, the age is 18. Therefore, the expression age >= 18 is true. Hence, you’ll see the following message in the console:

You can sign up.Code language: JavaScript (javascript)

The following example is the same as above except that the age is 18 instead of 16:

let age = 16;

if (age >= 18) {
  console.log('You can sign up.');
} else {
  console.log('You must be at least 18 to sign up.');
}Code language: JavaScript (javascript)

Output:

You must be at least 18 to sign up.Code language: JavaScript (javascript)

In this example, the age is 16. Therefore, the expression age >= 18 evaluates to false. Hence, the statement in the else branch executes that output the message to the console.

The following example uses a logical operator AND (&&) as the condition in the if block:

let age = 16;
let country = 'USA';

if (age >= 16 && country === 'USA') {
  console.log('You can get a driving license.');
} else {
  console.log('You are not eligible to get a driving license.');
}
Code language: JavaScript (javascript)

Because the age is 16 and the country is the USA, the following expression returns true.

age >= 16 && country === 'USA'Code language: JavaScript (javascript)

And you see the following output:

You can get a driving license.Code language: JavaScript (javascript)

Summary

  • Use the JavaScript if...else statement to execute a block if a condition is true and another block otherwise.

Quiz

Was this tutorial helpful ?