admin管理员组文章数量:1386695
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;
本文标签: conditional operatorJavascript shorthand for true or false in functionStack Overflow
版权声明:本文标题:conditional operator - Javascript shorthand for true or false in function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744546524a2611930.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论