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.