JavaScript Conditional Statement If ... Else

Source: Internet
Author: User
Tags date

Core tips: JavaScript Conditional Statement If ... Else

When you write code, you often need to perform different behaviors according to different conditions. You can use conditional statements in your code to accomplish this task.

In JavaScript, we can use the following conditional statements:

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 statement to choose to execute one of several block of code.
Switch statement
Use this statement to choose to execute one of several block of code.

If statement

<ptml> <body> <p> This example demonstrates an IF statement. </p> <p> If the browser time is less than 10, then ask you "Good Morning". </p> </body> </ptml>

This example shows an If statement.

If the browser time is less than 10, then ask you "Good Morning".


If...else statement

<ptml><br/><body><br/><br/><script type= "Text/javascript" ><br/>var d = New Date () <br/>var time = D.gethours () <br/><br/>if (Time <) <br/>{<br/> document.write ("<b> Good Morning </b>") <br/>}<br/>else<br/>{<br/>document.write ("<b > Wish you a Happy </b> ") <br/>}<br/></script><br/><br/><p> This example demonstrates If ... Else statement. </p><br/><br/><p> If the browser time is less than 10, then will ask you "Good morning", otherwise will greet you "wish you Happy". </p><br/><br/></body><br/></ptml><br/><br/>

This example shows If ... Else statement.

If the browser time is less than 10, then will ask you "Good morning", otherwise will say hello to you "wish you Happy".


If.. else If...else statement

<ptml><br/><body><br/><br/><script type= "Text/javascript" ><br/>var d = New Date () <br/>var time = D.gethours () <br/>if (time<10) <br/>{<br/>document.write ("<b >good morning</b> ") <br/>}<br/>else if (time>=10 && time<16) <br/>{<br/ >document.write ("<b>good day</b>") <br/>}<br/>else<br/>{<br/> document.write ("<b>hello world!</b>") <br/>}<br/></script><br/><br/> <p> This example demonstrates if. else If...else statement. </p><br/><br/></body><br/></ptml>

Good Day
This example shows if. else If...else statement.



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.