JavaScriptIf... Else declares the condition declaration in JavaScript to complete the behavior 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:
The 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
<Script type = "text/javascript"> // Write a "Good morning" greeting if // the time is less than 10 var d = new Date () var time = d. getHours () if (time <10) {document. write ("<B> Good morning </B>")} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Instance 2
<Script type = "text/javascript"> // Write "Lunch-time! "If the time is 11 var d = new Date () var time = d. getHours () if (time = 11) {document. write ("<B> Lunch-time! </B> ")} script
[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
<Script type = "text/javascript"> // If the time is less than 10, // you will get a "Good morning" greeting. // Otherwise you will get a "Good day" greeting. var d = new Date () var time = d. getHours () if (time <10) {document. write ("Good morning! ")} Else {document. write (" Good day! ")} Script
[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:
The 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:
<Script type = "text/javascript"> var d = new Date () var time = d. getHours () if (time <10) {document. write ("<B> Good morning </B>")} else if (time> 10 & time <16) {document. write ("<B> Good day </B>")} else {document. write ("<B> Hello World! </B> ")} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]