This forum is now read-only. Please use our new forums! Go to forums
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
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency