If...Else is somewhat similar to Decision Making.
Everyone has to make some kind of Decision in everyday life. Be it choosing a shirt or going to office by Bus or Cab.
Even in case of JavaScript, it has to make Decision.
And to make a Decision, If...Else is used by JavaScript.
There are three Decision Making statements used by JavaScript :
Let us see them in detail.
Let us understand if statement with the below example:
Say, you have a friend, who cannot remember anything. Now, you ask him to JavaScript to the market and get 1 kg Apple. You also tell him, if the price of Apple is less than 150 bucks, only then he should buy the Apple.
And since your friend has a weak memory. He takes out his notebook and writes :
Now, if your friend has to write the same thing in JavaScript. How would he write it?
Let us see with the below example.
<html> <body> <script language = "javascript" type = "text/javascript"> var price = 120 if (price < 150) { document.write("Buy the apples") } </script> </body> </html>
So, your friend writes the same thing in JavaScript for the same statement, "If the price of Apple is less than 150 then buy the apples".
if (price < 150) { document.write("Buy the apples") }
Quite Simple! Right?
It works in two steps :
var price = 120
if (price < 150) { document.write("Buy the apples") }
Now, that we have seen the if statement. Let us look at the syntax of if statement in detail.
if (price < 150) { document.write("Buy the apples") }
The above if statement begins with a if, followed by the actual condition (i.e. price < 150) enclosed by brackets ().
if (price < 150)
And the lines to be executed inside the blocks of if statement should be inside braces {}.
if (price < 150) { document.write("Buy the apples") }
Now, if the above condition (i.e. price < 150) is true. The document.write(...) statement is executed.
document.write("Buy the apples")
There can be lot of statements inside the if statements. What I meant is, there can be a lot of document.write statements. And they must be inside braces {}.
if (price < 150) { document.write("Buy the apples") document.write("The apples should be of good quality") document.write("Take the apples in a carry bag") }
So, in the above example we have seen your friend is happy because the price of apple is less than 150.
But what if the price of apple is more than 150. Say, the price of apple is 180. And he doesn't know what to do.
<html> <body> <script language = "javascript" type = "text/javascript"> var price = 180 if (price < 150) { document.write("Buy the apples") } </script> </body> </html>
And there is no output. Because the price of apple is 180 which is more than 150.
So, your friend comes back to you and you tell him to use else statement with if.
So, you tell your friend,
So, he writes the same thing in JavaScript using else statement.
<html> <body> <script language = "javascript" type = "text/javascript"> var price = 180 if (price < 150) { document.write("Buy the apples") } else { document.write("Do not buy apples") } </script> </body> </html>
So, to implement the line,
We have used the else statement along with if.
if (price < 150) { document.write("Buy the apples") } else { document.write("Do not buy apples") }
The syntax of else statement is almost same as if. Else statement uses the else keyword and its block begins with { and ends with }.
else
And its block begins with { and ends with }.
else { document.write("Do not buy apples") }
Now, your friend is happy. But you figured out that the shopkeeper is selling two types of apples for a fixed price.
1st kind of apple is 100 Bucks
2nd kind of apple is 200 Bucks.
And you give the new instruction to your friend.
So, this time you tell your friend,
And he writes the same in JavaScript.
<html> <body> <script language = "javascript" type = "text/javascript"> var price = 200 if (price == 100) { document.write("Buy 1st kind of apple") } else if (price == 200) { document.write("Buy 2nd kind of apple") } else { document.write("Do not buy apples") } </script> </body> </html>
So, in the else part, if we need to provide a condition (i.e. price == 200).
We need to use else if.
else if (price == 200) { document.write("Buy 2nd kind of apple") }
And the syntax of else if is just like if and else statement.
Say, you need to write a program to find the greatest of two numbers. And the input needs to be taken from the user.
<html> <body> <script language = "javascript" type = "text/javascript"> var firstNum = prompt("Enter the first number : ") var secondNum = prompt("Enter the second number : ") if (firstNum > secondNum) { document.write("The first number is greater than the second number.") } else if (firstNum < secondNum) { document.write("The second number is greater than the first number") } else { document.write("The first number and the second number are equal") } </script> </body> </html>
So, the output will be based on the numbers entered by the user.
firstNum = prompt("Enter the first number : ")
secondNum = prompt("Enter the second number : ")
if (firstNum > secondNum) { document.write("The first number is greater than the second number.") }
else if (firstNum < secondNum) { document.write("The second number is greater than the first number") }
The second number is greater than the first number
Just note that if both the conditions of if and else if is not satisfied, then we come to the else part.
else { document.write("The first number and the second number are equal") }
Which means both the values are equal.