Conditional execution means executing a code only when some condition is met. When developing an application, many times you need to perform some action only under a certain  condition. Example, display wrong password message only when password entered by user is incorrect.
Javascript provides following constructs to perform conditional execution.
1. if statement
2. if-else statement.
3. if-else if statement.
4. switch statement.
This section will discuss all these with examples.
1. if statement
if statement contains a condition followed by the code to be executed. This code is only executed when the condition of if statement returns true.
Syntax

if(condition) {
// code to execute
}

Condition given with if statement should return either true or false. It can be a simple condition such as x > 10, language == 'javascript', sum != 0 etc., or a complex condition containing logical operators such as x > 10 && language == 'javascript'. Example,

let language = 'javascript';
if(language == 'javascript') {
   console.log('Learning javascript');
}

2. if-else statement
This statement is required when you have two set of statements out of which atleast one and only one needs to be executed. Example, comparing password entered by user with the correct password. If the password is correct then allow the user to log in otherwise show invalid password error. One out of two needs to be executed. In such cases, if-else statement should be used.
Syntax

if(condition) {
// code to execute when condition is true
} else {
// code to execute when condition is false
}

if-else statement is equivalent to if statement followed by else block. When the condition followed by if statement is true, if block is executed otherwise else block is executed. Example,

let language = 'javascript';
if(language == 'javascript') {
   console.log('Learning javascript');
} else {
   console.log('This is some other language');
}

3. if-else if statement
When there are more than 2 or multiple conditions, then this statement is used. It consists of an if block with a condition followed by one or more else if blocks and an else block at the end. A block which satisfies the condition given after it, is executed. In case the conditions of all the blocks are false, else block is executed.
Syntax

if(condition 1) {
// code to execute when condition 1 is true
} else if(condition 2) {
// code to execute when this condition 2 is true
} else if(condition 3) {
// code to execute when this condition 3 is true
}  else {
// code to execute all conditions are false
}

Note that there is no condition after else since it is executed when conditions of all the preceding blocks are false.  Also it is not mandatory to have else block. It can be omitted in case there is no default code to be executed.
Example, a ticket vending application which takes the age of a person as input and calculates the ticket price.

let age = 25;
let price;
if(age < 5) {
  price = 0;
} else if(age >5 && age <=15) {
  price = 250;
} else if(age > 15 && age <= 60) {
  price = 500;
} else {
  price = 250;
}

4. switch statement
This is another conditional construct in javascript. Since this topic has many details to be discussed, it is explained in the next section here.

Leave a Reply