Skip to main content

Conditional Statements

In JavaScript you have the following conditional statements.

  • if
  • else
  • else if
  • switch

if Statement

In if statement there will be only one statement to be executed, so that there will be no else condition. Or other method is to use multiple or nested if condition inside the if statement. if statement is simplest statement to be used.

syntax

if (condition) {
statement;
}

The condition will check whether the expression is true or false.

Work out

An example of if loop is

var x = 10;
var y = 8;
if (x > 5 && y > 5) {
console.log("True");
}
// expected output: True

The loop increments the value of x till it satisfies the condition. If condition is not satisfied, loop terminates.

if-else Statement

The if statement executes a statement, if the condition is truthy, if the condition is false the else statement is executed.

Syntax

if (condition) {
statement1;
}
else {
statement2;
}

The statement1 is executed when the if condition is true. If the condition is false, the else statement is executed. More statements can be included by using nested if statements.

Work out

var x = 10;
var y = 3;
if (x > 5 && y > 5) {
console.log("True");
} else {
console.log("False");
}
// expected output: False

switch Statement

The switch statement will compare the expression with every case value and executes the statement of the matching case.

Syntax

switch(expression) {
case value1:
// Statement to be executed
break;
case value2:
// Statement to be executed
break;
case valueN:
// Statement to be executed
break;
default:
// Statement to be executed
break;
}

In switch statement expression is evaluated with the case value. If any of them matches, following statement of case is executed. The break statement helps to exit from the loop, if break is not declared all next cases will be executed including the default case. The default case to execute if there is no match for the value of expression and any of case values.

Work out

var x = 3;
var z = 6;
switch (z) {
case 1:
console.log("Number is " + 1);
break;
case 2:
console.log("Number is " + 2);
break;
case 3:
console.log("Number is " + 3);
break;
case 4:
console.log("Number is " + 4);
break;
case 5:
console.log("Number is " + 5);
break;
case 6:
console.log("Number is " + 6);
break;
default:
console.log("No match found");
break;
}

Loops

Loops are used to iterate over an array of data. It is used to repeat a block of code if specified condition is true. The different types of loops are:

  • while
  • do-while
  • for

while Statement

The while statement executes the statement as long as the condition fails. The condition is evaluated before execution of the statement.

Syntax

while (condition) {
statement;
}

At each time of statement execution, the condition is evaluated. If the condition is true, statement is executed. If the condition is false, loop will exit.

Work out

var x = 1;
while (x <= 10) {
console.log(x);
x = x + 1;
}
// expected output: 1 to 10
console.log("End");
// expected output: End

do-while Statement

The do-while is similar to the while loop except that the condition checks happen at the end of the loop.

Syntax

do {
statement
} while (condition)

Work out

var x = 1;
do {
console.log(x);
x = x + 1;
} while (x <= 10);
// expected output: 1 to 10
console.log("End");
// expected output: End

In while loop the statement is executed at least one time and re-execute if condition is true. When condition is false, the statement loop will exit.

for Statement

The for loop is slightly different from other loops because variable setup, condition checking and increment/decrement all are in single line as shown.

Syntax

for (initialization, condition, increment/decrement) {
statement
}

Work out

for (var i = 1; i <= 10; i++) {
console.log(i);
}
// expected output: 1 to 10
console.log("End");
// expected output: End

The value of i is printed till the condition is false. When the condition is false, the loop will exit.