Some of the selection statements in JavaScript:
1, if statement the code that executes the condition when the specified condition is true.
2, If...else ... Statement executes the code when the statement specifies that the condition is true, and executes other code if the condition is executed as false.
3, If...else if...else ... Statement is to select multiple code to execute together.
4, the switch statement selects multiple code to execute together.
If statement
if (condition) {
code that executes when the condition is true;
}
Example:
Copy Code code 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) {
executes code when the condition is true
}else{
code to execute when the condition is not true (the condition is false);
}
Example:
Copy Code code 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 If...else ... Statement
if (condition 1) {
executes the code when the condition 1 is true;
}else if (condition 2) {
executes the code when the condition 2 is true;
}else{
code to execute when both conditions 1 and 2 are not true;
}
Example:
Copy Code code 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>