Reputation: 413
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.
Upvotes: 2
Views: 4800
Reputation: 3483
Just to make it even shorter and allow for any comparison...
const trueFalse = (a, b) => a === b;
Upvotes: 0
Reputation: 191
(a few years after OP question but this would now also work)
ES6 shorthand would allow you to use a conditional operator:
const trueFalse = a === 1 ? true : false
Or even shorter but a bit less readable:
const trueFalse = a === 1
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
Upvotes: 1
Reputation: 119837
That would be:
function trueFalse(){
return a === 1;
}
Also, as much as possible, use strict comparison.
Upvotes: 5