DEV Community

Jin Choo
Jin Choo

Posted on

Returning Booleans

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);
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

This only works when you're returning a boolean from a function.

Top comments (2)

Collapse
 
frankwisniewski profile image
Frank Wisniewski

Arrow functions are ideal for this

const canVote = age => age >= 18
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jinchoo profile image
Jin Choo

Good point!