Is there a shorthand way of doing this so that the oute 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.
Is there a shorthand way of doing this so that the oute 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.
Share Improve this question edited Dec 30, 2022 at 16:49 Tristan F.-R. 4,2243 gold badges26 silver badges55 bronze badges asked Jun 14, 2013 at 15:10 martinmartin 4131 gold badge7 silver badges22 bronze badges 1-
3
a == 1
is already a boolean. – SLaks Commented Jun 14, 2013 at 15:13
3 Answers
Reset to default 5That would be:
function trueFalse(){
return a === 1;
}
Also, as much as possible, use strict parison.
(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/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
Just to make it even shorter and allow for any parison...
const trueFalse = (a, b) => a === b;