JavaScript If ... Else Declaration _ Basics

Source: Internet
Author: User
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]
Related Article

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.