This document provides examples of if, if-else, and if-else if-else statements in JavaScript. The if statement executes code if a condition is true. The if-else statement executes one block of code if the condition is true and another block if false. The if-else if-else statement checks multiple conditions and executes the block for the first true condition, or the else block if all are false.
1 of 4
More Related Content
Javascript survival2
1. Javascript Survival for Honesty and Humility Boolean Results AND OR NOT A B A&&B True True True True False False False True False False False False A B A||B True True True True False True False True True False False False A !(A) False True True False
2. The if statement if ( condition ) { code to be executed if condition is true } Example: <html><body> <script type="text/javascript"> var d = new Date(); var time = d.getHours(); if (time < 10) { document.write("<b>Good morning</b>"); } </script> <p>This example demonstrates the If statement.</p> <p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p></body></html>
3. The if-else statement if ( condition ) { code to be executed if condition is true } else { code to be executed if condition is not true } Example: <html><body><script type="text/javascript"> var d = new Date(); var time = d.getHours(); if (time < 10) { document.write("<b>Good morning</b>"); } else { document.write("<b>Good day</b>"); } </script> <p>This example demonstrates the If...Else statement.</p> <p>If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting. </p></body></html>
4. The if-else if-else statement if ( condition1 ) { code to be executed if condition1 is true } else if ( condition2 ) { code to be executed if condition2 is true } else { code to be executed if condition1 and condition2 are not true } Example: <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 && time<16) { document.write("<b>Good day</b>"); } else { document.write("<b>Hello World!</b>"); } </script>