The condition declaration in JavaScript is used to complete the action under different conditions.
Condition Statement
When writing code, you often need to perform different behaviors according to different conditions. You can use the condition declaration in the code to complete this task.
In JavaScript, we can use the following conditional declarations:
If statement
Run the code when a specified condition is set.
If... else Declaration
Execute the code when the specified condition is set. If the condition is not set, execute another code.
If... else Declaration
You can use this statement to execute one of several block codes.
Switch Declaration
You can use this statement to execute one of several block codes. If statement
If you want to execute code when the specified condition is set, you can use this statement.
Syntax:
Copy codeThe Code is as follows:
If (condition)
{
Run the code when the condition is set.
}
Note: Use lowercase letters. An error occurs when uppercase IF is used!
Instance 1
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Instance 2
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note: Use the equal sign (=) to compare variables!
Note: There is no else in the syntax. The code is executed only when the condition is true.
If... else Declaration
If you want to execute a piece of code when the condition is set, and execute another piece of code when the condition is not set, you can use the if... else declaration.
Syntax:
If (condition)
{
Run this code when the condition is set.
}
Else
{
Run this code when the condition is invalid.
}
Instance
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
If... else if... else Declaration
When you need to select one of multiple sets of code to run the program, use if... else declaration.
Syntax:
Copy codeThe Code is as follows:
If (condition 1)
{
Run the code when condition 1 is set.
}
Else if (condition 2)
{
Run the code when condition 2 is set.
}
Else
{
Run the code when neither condition 1 nor condition 2 is successful.
}
Instance:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]