• We just launched and are currently in beta. Join us as we build and grow the community.

If, Else, Else If Statement using JavaScript

MyPrefix

Content Interaction Strategist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
145
Likes
141
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. if

    (

    age <

    18

    )

    {
  4. {
  5. alert (

    " You are a minor."

    )

    ;
  6. }
  7. }
  8. </

    script>

This is the Output:
minor.png

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:
try_0.png

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:
adult.png


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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,491

323,080

323,089

Top