CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - If...Else

JavaScript - if…else Statement

The JavaScriptif…elsestatement executes a block of code when the specified condition is true. When the condition is false the else block will be executed. Theif-elsestatements can be used to control the flow of execution of a program based on different conditions.

While writing a program, there may be a situation when you need to adopt one out of a given set of paths. In such cases, you need to use conditional statements that allow your program to make correct decisions and perform the right actions.

JavaScript supports conditional statements used to perform different actions based on different conditions. Here we will explain theif…elsestatement.

Flow Chart of if-else

The following flow chart shows how the if-else statement works.

JavaScript supports the following forms ofif…elsestatement βˆ’

  • if statement
  • if…else statement
  • if…else if… statement.

JavaScript if statement

Theifstatement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.

Syntax

The syntax for a basic if statement is as follows βˆ’

JAVASCRIPT CODE
if (expression) {
   Statement(s) to be executed if expression is true
}

Here a JavaScript expression is evaluated. If the resulting value is true, the given statement(s) are executed. If the expression is false, then no statement would be not executed. Most of the times, you will use comparison operators while making decisions.

Example

Try the following example to understand how theifstatement works.

Preview

Output

JAVASCRIPT CODE
Qualifies for driving
Set the variable to different value and then try...

JavaScript if…else statement

The’if…else’statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way.

Syntax

JAVASCRIPT CODE
if (expression) {
   Statement(s) to be executed if expression is true
} else {
   Statement(s) to be executed if expression is false
}

Here JavaScript expression is evaluated. If the resulting value is true, the given statement(s) in the if block, are executed. If the expression is false, then the given statement(s) in the else block are executed.

Example

Try the following code to learn how to implement an if-else statement in JavaScript.

Preview

Output

JAVASCRIPT CODE
Does not qualify for driving
Set the variable to different value and then try...

JavaScript if…else if… statement

Theif…else if…statement (also called asif…else ladder)is an advanced form ofifelsethat allows JavaScript to make a correct decision out of several conditions.

Syntax

The syntax of an if-else-if statement is as follows βˆ’

JAVASCRIPT CODE
if (expression 1) {
   Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
   Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
   Statement(s) to be executed if expression 3 is true
} else {
   Statement(s) to be executed if no expression is true
}

There is nothing special about this code. It is just a series ofifstatements, where eachifis a part of theelseclause of the previous statement. Statement(s) are executed based on the true condition, if none of the conditions is true, then theelseblock is executed.

Example

Try the following code to learn how to implement an if-else-if statement in JavaScript.

Preview

Output

JAVASCRIPT CODE
Maths Book
Set the variable to different value and then try...