admin管理员组文章数量:1322850
There are a few ways to do this in javascript.
Foremost and most readable and flexible is probably:
if (a){
//b
}
else {
//c
}
Something else that only* works with assigning and is less readable is:
var foo = 'c';
if (a){
foo = 'b';
}
My main question, though, is about the last two methods I can think of:
var foo = a ? b : c;
var foo = a && b || c;
Are there any differences between these two expressions? Other than the readability which both lack.
*although you could assign foo to be a function, then execute it after the if statement.
There are a few ways to do this in javascript.
Foremost and most readable and flexible is probably:
if (a){
//b
}
else {
//c
}
Something else that only* works with assigning and is less readable is:
var foo = 'c';
if (a){
foo = 'b';
}
My main question, though, is about the last two methods I can think of:
var foo = a ? b : c;
var foo = a && b || c;
Are there any differences between these two expressions? Other than the readability which both lack.
*although you could assign foo to be a function, then execute it after the if statement.
Share Improve this question edited Mar 25, 2009 at 7:28 AnnanFay asked Mar 24, 2009 at 19:13 AnnanFayAnnanFay 9,75915 gold badges67 silver badges89 bronze badges 2- To be pedantic: you are technically discussing the conditional operator which just happens to be a ternary operator (that is an operator that operates on three elements). – Andrew Hare Commented Mar 24, 2009 at 19:25
- For short statements, the conditional operator can be more "readable" than any full-fledged if construct. – Svante Commented Mar 24, 2009 at 22:01
4 Answers
Reset to default 9Suppose:
var a = false, b = '', c = 'bar';
Then:
var foo = a ? b : c; // foo == ''
var foo = a && b || c; // foo == 'bar'
The two are not equivalent; you should never use the boolean operators in place of the conditional operator. Like other answerers, I also am of the opinion that the conditional operator does not lack readability for simple expressions.
The ternary operator is certainly readable to me. Even moreso than the first example, since concise, logical code is always easier to understand than many lines of control code that do the same thing.
I don't agree the first lacks readability as long as it is used correctly, IOW a, b and c are simple expressions themselves. This operator does get abused though in circumstances it ought not.
Similarly the second expression, using the result foo as anything other than a boolean would not be good. Using the boolean operators to return non-boolean values because thats the way they work is just confusing. However as a boolean expression its reasonable.
var foo = a ? b : c; // foo is b when a is true else c.
var foo = a && b || c; // foo is true if a and b are true or c is true.
They do not evaluate to the same result as the latter is a boolean expression, not using a ternary operator.
本文标签:
版权声明:本文标题:javascript - The ternary operator and if A, B, else C. Are there any important differences? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740913957a2306738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论