Basic Programming Tutorials

JavaScript if else

Welcome to the JavaScript Tutorials, we already covered many things. If you missed any, then go and check the covered topics. Today we will talk about JavaScript If else, the ‘JavaScript If Else’ will help you to answer the conditional situation. The conditional statements will let you write a code that will take a decision, and show the results.

Let’s see the JavaScript If else conditional statements that you will be used to make the different decisions:

  • If statement – It will allow the program to execute the statement, only if the condition is true.
  • If else statement – It will allow the program to execute the code in either way. If it is true, then perform this statement and if it is false, then perform the alternate statement.
  • Else if statement – You can use it, if your first condition is false.

JavaScript If Else – Examples

Now we will try to use the above conditional statements one by one to understand its use.

JavaScript If Statement – Example

Now we will start with If statement, we have written a code where you will see results only when the myVote’s value is ‘sharplesson’. In other words, just vote us, else nothing will happen (kidding). But yes, if your value is other than sharplesson, then blank screen will only come.

<html>

<head></head>

<body>

<script type="text/javascript">

<!--

var myVote = "sharplesson";

if(myVote == "sharplesson"){

document.write("You have voted for us!");

}

//-->

</script>

</body>

</html>

The output for the above code will be:

You have voted for us!

We just used if statement, and if it will be true, then only it will show you the above result. Try by putting some other ‘string’ or value in If statement. The only result will be shown is Blank screen.

Note: The only drawback of the If statement is, it will not execute, if the condition will be false.

JavaScript If Else – Example

Now we will see the use of JavaScript If else statement, we have taken a very simple example. If your ‘if’ condition is true, then it will execute the relevant code. In case your if condition is false, then it will execute the else statement.

<html >

<head>

</head>

<body>

<script type="text/javascript">

<!--

var myVote = "sharp";

if(myVote == "sharplesson"){

document.write("You have voted for us!");

}else{

document.write(" you have not voted for SharpLesson")

}

//-->

</script>

</body>

</html>

The output will be:

Here out of two, only one output will come. But for your understanding, we will show and explain both.

True condition:

If the myVote’s value is “ sharplesson”, then the result will be:

You have voted for us!

False condition:

You have not voted for SharpLesson

You can clearly see that, what will happen if the value will not match or match. It is best to use, because you have two options to choose from.

Note: Unlike if statement, JavaScript If else statement will execute and display the result.

JavaScript If Else if – Example

Now we will see, how to use JavaScript If Else If statement. Again we have used a very simple example to understand the use of else if statement. If your myVote’s value is SharpLesson, then it will execute the result. If your myVote’s value is ‘pal’, then it will execute the relevant result, and if both the statements ‘if’ and ‘ else if’ will fail to stand, then it will execute the ‘else’ statement.

<html >
<head></head>

<body>

<script type="text/javascript">

<!--

var myVote = "sharplesson";

if(myVote == "sharplesson"){
document.write("You have voted for sharplesson!");

}else if(myVote == "pal"){

document.write("You have not voted for sharplesson");

} else {

document.write("Where is your vote? ");

}

//-->

</script>

</body>

</html>

The output of the above program will be as per the true condition:

True – if statement

You have voted for sharplesson!

True – else if statement

You have not voted for sharplesson

Else – last option

Where is your vote?

Whichever condition is true, it will execute the relevant result.

Note: You can use JavaScript if else statement, if you want to test more than one condition.

Conclusion

We have just covered JavaScript If Else conditional statements with an easy to understand example. You can use any statement or together if needed. The last thing that we will ask you to do is, just practice if else statement to perfection.