Just like any other programming language JavaScript also have conditional statements like if…else. These conditional statements are an essential part of programming as they have logic to execute two or more different set of statements.
JavaScript supports the following form of if…else statements –
The if statement in it’s a standalone avatar, will run a set of the statement contained inside its curly brackets to run if it evaluates to true.
var age = 22; if(age >= 21) { console.log('You can vote in the election'); } //You can vote in the election
In the above example, we initialize age to 22. Now, in the if statement we check whether age >= 21 and it is true. So, the statement 'You can vote in the election' will be printed in the console.
Sometimes we want to execute a set of statements if the condition checked in the if is false. This is where the variation of if statement i.e. the if…else comes into play.
var age = 18; if(age >= 21) { console.log('You can vote in the election'); } else { console.log('You are too young to vote'); } // You are too young to vote
We have refactored the if statement code to run the else part if the statement age >= 21, returns false. In the above example we initialize age to 18. Now, in the if statement we check whether age >= 21 and it is false. So, the statement 'You are too young to vote' will be printed in the console.
Sometimes we need to check many set of conditions and depending on it to execute that set of statements. This is where the variation of if statement i.e. the if…else if…else comes into play. Consider the below example.
var age = 9; if(age >= 21) { console.log('You can vote in the election'); } else if(age >= 15) { console.log('You are a teenager and cannot vote'); } else if(age >= 10) { console.log('You are too young to vote'); } else { console.log('You are still a baby'); } // You are still a baby
We have refactored the if statement code to run two else if part and one else part. In the above example, we initialize age to 9. Now, in the if statement we check whether age >= 21 and it is false. Then we go to the next else if part and check whether age >= 15 and it is false again. Then we go to the next else if part and check whether age >= 10 and it is false again.
So, we just print the statement in else part i.e. 'You are still a baby'.
I have learned many things from this article. It is beneficial for me. Thank you!
Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..
This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.
Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.
You have shared great information with me i appreciate your work!
Build various types of web applications,command-line application,etc....
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *