Aisha Wahab O.
3 min readMar 1, 2024

Hiiii. Let’s continue with our series on Javascript. This week’s write-up is focusing on conditional statements in Javascript. Shall we?

Conditional statements, also known as "if else" statements, are a type of control flow statement in Javascript that let us perform different actions based on the value of an expression. During programming, often times, we want to perform various actions based on different conditions, it is these conditional statements that make it possible for us to do so.

Example: Let’s compare two values and print the greater one.

let num1 = 6;
let num2 = 4;

let result = num1 > num2;

if(result)
console.log("num1 is greater");

console.log("Bye...");

//num1 is greater
Bye...

The output is as above because the result for ‘num1 > num2' is true.

Another example:

let num1 = 3;
let num2 = 4;

let result = num1 > num2;

if(result)
console.log("num1 is greater");

console.log("Bye...");

//Bye...

The output for the above is ‘Bye...’. because the result isn’t executed as num1 is not greater than num2. Remember, it’s outside the ‘if…’ block. The ‘if…’ block is

if(result)
console.log("num1 is greater");

The result of comparison of the two variables is false, thus the output isn’t executed.

Photo by Gustas Brazaitis on Unsplash

However, this can be solved by the following. Example:

let num1 = 3;
let num2 = 4;

let result = num1 > num2;

if(result)
console.log("num1 is greater");
else
console.log("num2 is greater");

console.log("Bye...");

//num1 is greater
Bye...

The output shows that because it’s outside the ‘if…’ condition. If it’s true, it’d print the first block and if the result is false, the second block would be executed.

What if I want to evaluate multiple statements in the ‘if…’ or ‘else…’ block?

Example:

let num1 = 3;
let num2 = 4;

let result = num1 > num2;

if(result)
console.log("num1 is greater");
else
console.log("num2 is greater");
console.log("yeeee");

console.log("Bye...");

//num2 is greater
yeeee
Bye...

By default, every block should have only one statement. What this means is that ‘if…’ , ‘else…’ etc should have only one statement. To add, the ‘yeee’ to the ‘else…’ block, we need to put it in parenthesis. In essence, multiple statements require parenthesis.

Example:

let num1 = 6;
let num2 = 4;


if(num1 > num2){
console.log("num1 is greater");
}
else{
console.log("num2 is greater");
console.log("yeeee");
}
console.log("Bye...");
//num1 is greater
Bye...

Note: we don’t have ‘yeeee’ anymore because it’s a part of the else… block.

Comparing three values.

Remember, the operator used to compare three logical operations is the ‘&&’. To compare, we’d make use of the ‘And’ operator because we want two expressions to be true.

We’d also make use of the ‘else…if’. It is used when the first condition is false.

let num1 = 6;
let num2 = 4;
let num3 = 7;


if(num1 > num2 && num1 > num3){
console.log("num1 is greatest");
}
else if(num2 > num1 && num2 > num3){
console.log("num2 is greatest");
}
else{console.log("num3 is greatest");
}

console.log("Bye...");
//num3 is greatest
Bye...

First, we compare if num1 is greatest, then compare if num2 is greatest. And we know that num1 and num2 are not greatest, hence we print out num3.

If we replace num2 with 9, then output = num2 is greatest. Similarly, if we replace num1 with 10, output = num1 is greatest.

However, we should note that when we compared the above Javascript equation, num1 isn’t greatest, thus it’s false. As a result, the only competition remains between num2 and num3. We do not need to compare num1 and num2 again. Thus, it should be written as follows:

let num1 = 6;
let num2 = 4;
let num3 = 7;


if(num1 > num2 && num1 > num3){
console.log("num1 is greatest");
}
else if(num2 > num3){
console.log("num2 is greatest");
}
else{console.log("num3 is greatest");
}

console.log("Bye...");
//num3 is greatest
Bye...

In summary, we have learnt that conditional statements in Javascript are in 3 ways.

“If” statements: they are used to specify execution for a block of code if a condition is true.
“Else” statements: it specifies the execution for a block of code if the condition is false.
“Else if” statements: this specifies a new test if the first condition is false.

Kindly leave a clap, comment and share if you find it beneficial.

See you next time!

Aisha Wahab O.
Aisha Wahab O.

Written by Aisha Wahab O.

I currently write on JavaScript. .

No responses yet