If you're returning a boolean (true
or false
), sometimes it can be redundant. Example:
function canVote(age) {
if (age >= 18 ) {
return true;
} else {
return false;
}
}
canVote(18);
The code above is redundant because grade >= 18
already evaluates to true
or false
depending on the age.
You can use without if/else
, which will always return a boolean. Example:
function canVote(age) {
return age >= 18;
}
This only works when you're returning a boolean from a function.
Top comments (2)
Arrow functions are ideal for this
Good point!