top

Search

JavaScript Tutorial

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 –if statementif…else statementif…else if…else statementif statementThe 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 electionIn 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.if..else statementSometimes 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 voteWe 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.if..else if…else statementSometimes 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 babyWe 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'.
logo

JavaScript Tutorial

If…else

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 –

  • if statement
  • if…else statement
  • if…else if…else statement

if statement

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.

if..else statement

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.

if..else if…else statement

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'.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Aditya

The tutorial provides the more information JavaScript this provides the end to end information about JS and also they provides the more detailed concepts of loops, functions, objects, arrays this will use us to buld the effective web site. Thanks for the team good job.

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More