This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by akshatpradhan
almost 12 years

if else if vs if if

What’s the advantage of using if else if vs if if

if else if

var isDivisible = function (x, y) {
    if (x % y === 0)
      return true;
    else if( x % y !== 0)
      return false;
};

if if

var isDivisible = function (x, y) {
    if (x % y === 0)
      return true;
    if( x % y !== 0)
      return false;
};

Answer 4f7cd13adc8889000300748b

0 votes

Permalink

They are more or less the same, but there is one subtle difference: in the first case, only one of the two statements can be executed while in the second case it is possible for both of them to be executed.

In this example, it does not matter since the two conditions are mutually exclusive, but it may become important if it is possible for both conditions to be true. In that case, “if else if” would only execute the first “if” statement and “if if” would execute them both in sequence.

points
Submitted by holyelk3
almost 12 years