Introduction to the use of if Statements in javascript

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.