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:
JavaScript supports the following if.else order forms:
- If statement
- If . else statement
- 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 :