If ... else command in JavaScript

While writing a program, there may be a situation when you need to follow one of a set of choices. In this case, you need to use conditional commands that allow your program to control the flow correctly and perform the right actions.

While writing a program, there may be a situation when you need to follow one of a set of choices. In this case, you need to use conditional commands that allow your program to control the flow correctly and perform the right actions.

JavaScript supports conditional commands used to perform various actions on the basis of different conditions. Below we will explain the if . else command.

The diagram performs the if . else command

The diagram below shows the if . else command to work:

If ... else command in JavaScript Picture 1If ... else command in JavaScript Picture 1

JavaScript supports the following if.else order forms:

  1. If statement
  2. If . else statement
  3. If . else if . command

If statement

The if statement is a basic control command that allows JavaScript to control the flow and execute commands according to conditions.

Syntax

The syntax for a basic if statement is:

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

Here, a JavaScript expression is evaluated. If the result is true, the provided command is executed. If the expression is false, no command will be executed. Most of the time, you will use comparison operators while controlling flow.

For example

Try the following example to understand how to work if :

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

 Qualifies for driving 
Đặt biến vào giá trị khác nhau và thử thử .

If . else statement

The 'if . else ' command is another control command pattern that allows JavaScript to execute commands in a more controlled manner.

Syntax

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

Here, JavaScript expressions are evaluated. If the result value is true, the command provided in the if block is executed. If the expression is false, then the given command in the else block is executed.

For example

Try the following example to understand how if . else commands work in JavaScript.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

 Does not qualify for driving 
Đặt biến vào giá trị khác nhau và Then try . If . else if .

If . else if . command is an advanced if . else control command that allows JavaScript to make an accurate decision in some conditions.

Syntax

Syntax of if-else-if statement is as follows:

 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 a series of if statements, each if is part of the else clause of the previous command. The commands are executed on a true condition, if there is no true condition, then the else block is executed.

For example

Try the following example to learn how to execute an if-else-if statement in JavaScript.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

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

Follow tutorialspoint

Previous article: Operator in JavaScript

Next lesson: Switch Case command in JavaScript

4.5 ★ | 2 Vote