If, Else, Else If Statement using JavaScript

In this tutorial, we are going to learn about If, Else, Else If Statement using JavaScript. These all statement execute with a condition. With the use If, Else, and Else If, you can make your program knowledgeable. If Statement - JavaScript This is If Statement. It will execute if the condition is True.
  1. <script>
  2. var age = 16;
  3.  
  4. if (age < 18) {
  5. {
  6. alert (" You are a minor.");
  7. }
  8.  }
  9. </script>
This is the Output: Result - Minor If... Else Statement - JavaScript The example above will execute only if the condition is true. If.. Else statement it will execute another statement if the condition is false.
  1. <script>
  2. var age = 0;
  3. if (age < 18) {
  4. if (age > 0)
  5. {
  6. alert (" You are a minor.");
  7. }       else {
  8. alert ("Input Correct Details. Try Again.");
  9. }
  10. }
  11. </script>
This is the Output: Result - Try If... Else If... Else Statement - JavaScript You can use this statement if you want multiple conditions.
  1. <script>
  2. var age = 19;
  3. if (age < 18) {
  4. if (age > 0)
  5. {
  6. alert (" You are a minor.");
  7. }       else {
  8. alert ("Input Correct Details. Try Again.");
  9. }
  10. }       else if (age >= 18)
  11. {
  12. alert ("You are already an Adult." )
  13. }
  14. </script>
This is the Output: Result - Adult Hope that this simple tutorial that I created may help you to your future projects. Also, for the beginners who want to learn basic of coding in JavaScript. Related Code: PHP If…Else Statements Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment