JavaScript Conditions and if, else if and else Statements

About JavaScript If Statement

In JavaScript, the 'if' conditional statement is used for conditional execution. It evaluates a condition and executes a block of code if the condition is true.

Boolean expression is often used as condition of 'if' statement. If it evaluates to true, the code inside the 'if' block runs; otherwise, the code inside the 'else' block runs.

Syntax of 'if' Statement

The general syntax of 'if' statement is as follows:

If the condition is true, the code block under 'if' statement will be executed. Otherwise, the program flow will move to the next level.

Example of 'if' Statement
Output of the above example
The person is a voter.
                                
Explanation of the code
  • let age = 19;: This line declares a variable named 'age' and assigns a number 19.
  • if (age >= 18): 'if' is the keyword of 'if' statement. age >= 18 is the condition of 'if' statement which returns Boolean value 'true' as the condition is true.
  • console.log("The person is a voter.");: This is the code block under 'if' statement. As the 'if' condition is true, this line will executed. It will display "The person is a voter." in the console.

Syntax of If...Else Statement

The general syntax of an if...else statement is as follows:

In this case, if the condition is true, the code block under 'if' condition will be executed. Otherwise, the code block under 'else' statement will be executed.

Example of 'if...else' Statement
Output of the above example
The person is not a voter.
                            
Explanation of the code
  • let age = 17;: This line declares a variable named 'age' and assigns a number 17.
  • if (age >= 18): 'if' is the keyword of 'if' statement. age >= 18 is the condition of 'if' statement which returns Boolean value 'false' as the condition is false. The code block under 'else' block will be executed
  • console.log("The person is a voter.");: This is the code block under 'if' statement.
  • else: 'else' is the keyword of 'else' statement. In this example, the 'else' block will be executed as the 'if' statement condition is false.
  • console.log("The person is not a voter.");: This is the code block under 'else' statement.

Syntax of If...else if...Else Statement

The syntax of an if...else if...else statement is as follows:

In this case, if the 'if' condition is true, the code block under 'if' condition will be executed. If the 'else if' condition is true, the code block under 'else if' condition will be executed. Otherwise, the code block under 'else' statement will be executed. You can use as many as 'else if' conditions requires.

Example of 'if...else if...else' Statement
Output of the above example
The person is a senior voter.
                            
Explanation of the code
  • let age = 60;: This line declares a variable named 'age' and assigns a number 17.
  • if (age < 18): 'if' is the keyword of 'if' statement. age < 18 is the condition of 'if' statement which returns Boolean value 'false' as the condition is false. The code block under 'if' block will not be executed.
  • console.log("The person is not a voter.");: This is the code block under 'if' statement.
  • else if(age > 32): 'else if' is the keyword of 'else if' statement. age > 32 is the condition of 'else if' statement which returns Boolean value 'true' as the condition is true. The code block under 'else if' block will be executed.
  • console.log("The person is a senior voter.");: This is the code block under 'else if' statement.
  • else: 'else' is the keyword of 'else' statement. In this example, the 'else' block will not be executed as the 'else if' statement condition is true.
  • console.log("The person is a young voter.");: This is the code block under 'else' statement.

JavaScript Compiler For Live Practice

Practice the above JavaScript code in the following JavaScript Editor. Create more JavaScript 'if' conditional statements in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.

JavaScript logo JavaScript Editor
JavaScript Code Output