Document directory
- Instance 1
- Instance 2
- Syntax:
- Instance
- Syntax:
- Instance:
The Condition Statement in Javascript is used to complete the action under different conditions.
Instance
-
If statement
-
How to compile an if statement.
-
If... else statement
-
How to compile the IF... else statement.
-
If. Else if... else statement
-
How to compile the IF... else if... else statement
-
Random Link
-
This example shows a random link. When you click this link, a random website is opened.
Condition Statement
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 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
If you want to run the code when the specified condition is set, you can use this statement.
Syntax:
if
(Condition) {execute 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 10var d=new Date()var time=d.getHours()if
(time<10) {document.write("<b>Good morning</b>")}</script>
Instance 2
<script type="text/javascript">//Write "Lunch-time!" if the time is 11var d=new Date()var time=d.getHours()if
(time==11) {document.write("<b>Lunch-time!</b>")}</script>
Note: Use the equal sign (=)ComparisonVariable!
Note: There is no else in the syntax.Only when the condition is true, The code will be executed.
If... else statement
If you want to execute a piece of code when the condition is set to true, and execute another piece of code when the condition is not met, you can use the IF... else statement.
Syntax:
if
(Condition) {execute this code when the condition is set}else
{Execute 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>
If... else if... else statement
When you need to select one of multiple sets of code to run the program, use the IF... else if... else statement.
Syntax:
if
(Condition 1) {Execution Code when condition 1 is set}else if
(Condition 2) {Execution Code when condition 2 is set}else
{Code execution 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>