Some selection statements in javascript:
1. if statement when the specified condition is true, the code for this condition is executed.
2. if... else... statement when the condition is set to true, the code is executed. if the condition is set to false, other code is executed.
3. if... else... the statement is executed with multiple codes.
4. Select multiple codes to execute the switch statement.
If statement
If (condition ){
Code executed when the condition is true;
}
Example:
Copy codeThe Code is as follows:
<Script>
Function myFunction ()
{
Var x = "";
Var time = new Date (). getHours ();
If (time <20)
{
X = "Good day ";
}
Document. getElementById ("demo"). innerHTML = x;
}
</Script>
If... else... statement
If (condition ){
When the condition is true, run the code
} Else {
Code executed when the condition is not true (the condition is false;
}
Example:
Copy codeThe Code is as follows:
<Script>
Function myFunction ()
{
Var x = "";
Var time = new Date (). getHours ();
If (time <20)
{
X = "Good day ";
}
Else
{
X = "Good evening ";
}
Document. getElementById ("demo"). innerHTML = x;
}
</Script>
If... else... statement
If (condition 1 ){
When condition 1 is true, the code is executed;
} Else if (condition 2 ){
When condition 2 is true, the code is executed;
} Else {
Code executed when neither condition 1 nor condition 2 is true;
}
Example:
Copy codeThe Code is as follows:
<Script>
Function myFunction ()
{
Var x = "";
Var time = new Date (). getHours ();
If (time <10)
{
X = "Good morning ";
}
Else if (time <20)
{
X = "Good day ";
}
Else
{
X = "Good evening ";
}
Document. getElementById ("demo"). innerHTML = x;
}
</Script>