Conditionals
Conditionals are used to determine if a block of code should or should not be executed depending on a given set of conditions.
if / else if / else
Let's start with a simple example of checking the age of a person.
var age = 17;
if (age < 18) {
console.log('Underage');
} else {
console.log('Overage');
}
// The console will show: 'Underage'
In this example we only have one condition and if it's not true we jump directly to the else statement. But we could have more conditions by using else if statements.
var age = 70;
if (age < 18) {
console.log('Underage');
} else if (age >= 65) {
console.log('Senior');
} else {
console.log('Adult');
}
// The console will show: 'Senior'
No matter how many conditions we write, when one condition returns true, all the other conditions are ignored by Javascript. If none of the conditions return true, the code inside the else statement will be executed, although the else statement is not mandatory.
Testing multiple conditions at once with and / or
Let's analyze the grade and absences of a student and determine if he's been approved in the course. To be approved the student needs the grade to be 7 or higher and the number of absences to be 5 or lower.
grade = 7;
absences = 3;
// Solving the problem with && (and):
if (grade >= 7 && absences <= 5) {
console.log( 'The student has been approved' );
} else {
console.log( 'The student has failed' );
}
// Solving the problem with || (or):
if (grade < 7 || absences > 5) {
console.log( 'The student has failed' );
} else {
console.log( 'The student has been approved' );
}
// Both ways are correct
// change the values of the variables to test
Evaluating values as conditions
In Javascript, just like Python, PHP etc., we can evaluate values as true or false without booleans. Javascript considers to be false:
All other values, numbers, strings, objects and arrays are considered to be true.
Therefore, it's common to use conditionals like this:
var name = "";
if (name) {
console.log("The name is " + name);
} else {
console.log("The name has not been informed");
}
// The console will show "The name has not been informed"
// This method could be used to test if a form field is empty, for example
Ternary If
There's another way of writing conditionals using the ternary notation.
The syntax is: condition ? code to be executed if true : code to be executed if false;
So rewriting the example above we have:
var name = "";
console.log( name ? "The name is " + name : "The name has not been informed" );
// As you can see, we can write less code to get to the same result.
Using else if in ternary conditionals
We can declare else if statements with the ternary notation by placing the next condition where the else would be and then just continuing with the same structure.
The syntax is: condition ? code to be executed if true : another condition ? code to be executed if true : code to be executed if false;
Let's rewrite the age example from above.
var age = 70;
console.log( age < 18 ? 'Underage' : age >= 65 ? 'Senior' : 'Adult' );
This time the fewer lines of code we achieve comes with a trade-off: We are making the code readability worse. For this reason, ternary conditionals should be used only for simple if / else structures.