Conditional declarations in JavaScript are used to complete behavior under different conditions.
Conditional declaration
When you write code, you often need to perform different behaviors according to different conditions. You can use conditional declarations in your code to accomplish this task.
In JavaScript, we can use the following conditions to declare:
If statement
Executes code when a specified condition is set up.
If...else statement
Executes code when the specified condition is set up, and executes additional code when the condition is not valid.
If...else If....else Statement
Use this declaration to optionally execute one of several block codes.
Switch declaration
Use this declaration to optionally execute one of several block codes. If statement
You can use this declaration if you want the specified condition to execute code when it is set up.
Grammar:
Copy Code code as follows:
if (condition)
{
Execute code when conditions are set
}
Note: Please use lowercase letters. Using uppercase if can be an error!
Instance 1
<script type= "Text/javascript" >//write a "Good morning" greeting if//the time is less than Var d=new Date () var time=d.gethours () if (time<10) {document.write ("<b>good morning</b>")} </script> ;
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Instance 2
<script type= "Text/javascript" >//write "lunch-time!" If the time is one var d=new Date () var time=d.gethours () if (time==11) {document.write ("<b>Lunch-time!</b>")} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Note: Use the double equal sign (= =) to compare variables!
Note: There is no else in the syntax. The code executes only if the condition is ture.
If...else statement
If you want to execute a piece of code when the condition is set up and execute another piece of code when the condition is not established, you can use the If....else declaration.
Grammar:
if (condition)
{
Execute this code when the condition is set
}
Else
{
Execute this code when the condition is not true
}
Instance
<script type= "Text/javascript" >//if the time is less than,//you'll get a "Good morning" greeting. Otherwise you'll get a ' good day ' greeting. var d = new Date () var time = D.gethours () if (Time <) {document.write ("good morning!") else {document.write ("good day!") } </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
If...else If...else Statement
When you need to select a set of multiple sets of code to run, use the If....else if...else declaration.
Grammar:
Copy Code code as follows:
if (condition 1)
{
Condition 10% Execute code immediately
}
else if (condition 2)
{
Condition 20% Execute code immediately
}
Else
{
Execute code when both condition 1 and Condition 2 are not true
}
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 && Amp TIME<16) {document.write ("<b>good day</b>")} else {document.write ("<b>hello World!< /b> ")} </script>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]