In this article you will learn the basics of JavaScript conditions and create your conditional examples in JavaScript .
About Conditional Statement
First of all we write the code after that we want to perform the different
actions for different decisions. We can use conditional statements in our code. Conditional statements in JavaScript are used to perform different actions based on different conditions.
There are following types of Conditional Statements:-
This example is if - else statement. It display the first of all the sum of two numbers and check the condition after that it execute the code.
| <html> <head> <script language="javascript"> function showAlert() { var a=20; var b=10; var sum=0; sum=a+b; document.write(" sum= "+sum); if(sum==3) { document.write("Condition is true"); document.write("This is my first if statement program"); } false { document.write("Condition is false"); document.write(" This is my first if -else statement program"); } } </script> </head> <body> <script language="javascript"> showAlert(); </script> </body> </html> |
This example is display to all months in a year for using the switch case:-
| <body> <script type="text/javascript"> var n=0; n=prompt("Enter a number between 1 to 12:") switch(n) { case(n="1"): document.write("January"); break case(n="2"): document.write("Febuary"); break case(n="3"): document.write("March"); break case(n="4"): document.write("April"); break case(n="5"): document.write("May"); break case(n="6"): document.write("June"); break case(n="7"): document.write("July"); break case(n="8"): document.write("August"); break case(n="9"): document.write("September"); break case(n="10"): document.write("October"); break case(n="11"): document.write("November"); break default: document.write("December"); break } </script> </body> |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Conditional Examples(if - else- switch case) in JavaScript View All Comments
Post your Comment