Search code examples
javascriptconditional-operator

Javascript shorthand for true or false in function


Is there a shorthand way of doing this so that the outcome is always true or false?

function trueFalse() {
  if( a == 1 ) {
    return true;
  } else {
    return false;
  }
}

Something like return true:false; and no need for the else section?

Thanks.


Solution

  • That would be:

    function trueFalse(){
      return a === 1;
    }
    

    Also, as much as possible, use strict comparison.