Press ESC to close

if else JavaScript

0 1409

In this lesson we study the instruction if else in JavaScript and we will do some pratical examples of use.

Recall that conditional statements allow you to check a condition, if this condition is true you execute some statements, otherwise you can execute others.

if else JavaScript – first example

In this first example we will compare if two values ​​are equal.

Initialize two variables a and b with integer values ​​and check if they contain the same value.

So let’s initialize the two variables, for example: a = 5 and b = 6 and then use the conditional if statement.

The syntax of the if statement requires that the test to be verified is specified in round brackets. The test returns a Boolean value, that is, true or false.

In this example, given that a = 5 and b = 6, the test (a == b) will be false and therefore the instructions contained in braces will never be executed.

var a = 5, b = 6;

if(a == b){
 document.write('The values of ' + a + ' and ' + b + ' are equals');

if else JavaScript – second example

The if statement can therefore be used alone or together with the else statement. In this way, if the condition specified in the if is found to be false, the instructions specified in the else can be carried out.

Given two integers a and b check whether they contain the same value or not.

So let’s take another example, which represents a variant of the previous one:

var a = 5, b = 6;

if(a == b){
   document.write('The values of ' + a + ' and ' + b + ' are equals');
else {
    document.write('The values of ' + a + ' and ' + b + ' are not equals');

In this example, if a and b are equal, the instruction in curly brackets of the if is executed, otherwise the instruction contained in the else is executed.

if else JavaScript – third example

Furthermore, the if else statement can be extended with the else if clause.

Let’s see a simple example of use:

var a = 5, b = 6;

if(a == b){
  document.write('The values of ' + a + ' and ' + b + ' are equals');
else if (a > b){
  document.write('The value ' + a + ' is ' +  ' major than ' + b);
else {
  document.write('The value  ' + b + ' is ' +  ' major than ' + a);

In this example, therefore:

  • if a and b are equal, the instructions contained in the braces of the first if will be executed.
  • Otherwise (that is, if the first test is false) it is evaluated if a is greater than b. If the condition returns a Boolean value of true, it will do what is specified in the curly brackets.
  • Otherwise, if the second condition is also false, what specified in the last else will be executed. Clearly this is done without inserting a condition, as it is taken for granted at this point that a is less than b.


These are just simple examples of if else statements in JavaScript, in the next lesson we will cover some more.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Other stories

JavaScript if statement

Next Story

Variables JavaScript

Previous Story