When writing code, you often need to perform different behaviors according to different conditions. You can use conditional statements in the code to complete this task.
In JavaScript, we can use the following conditional statements:
-
If statement
-
Run the code when a specified condition is set.
-
If... else statement
-
Execute the code when the specified condition is set. If the condition is not set, execute another code.
-
If... else statement
-
You can use this statement to execute one of several block codes.
-
Switch statement
-
You can use this statement to execute one of several block codes.
If statement
<Script type = "text/javascript"> var d = new Date () var time = d. getHours () if (time <10) {document. write ("Good morning")} </Script>This example demonstrates the If statement.
If the browser time is less than 10, you will be asked "good morning ".