admin管理员组文章数量:1417033
I am making small utility to pile javascript block using C#. I am trying to understand ternary operator's execution flow. Now when I am running a javascript using Chrome or Firefox :
var k = 27;
var o = 78;
var a = k < 100 ? o+=2 > 11 ? "T" : "F" : o < 100 ? "J" : "P";
alert(a);
It should have give me result "T" or "F" if "o+=2" returns false. But instead of those it returns "78F". Can anyone please explain me whats the logic behind it.
I am making small utility to pile javascript block using C#. I am trying to understand ternary operator's execution flow. Now when I am running a javascript using Chrome or Firefox :
var k = 27;
var o = 78;
var a = k < 100 ? o+=2 > 11 ? "T" : "F" : o < 100 ? "J" : "P";
alert(a);
It should have give me result "T" or "F" if "o+=2" returns false. But instead of those it returns "78F". Can anyone please explain me whats the logic behind it.
Share Improve this question asked Mar 11, 2016 at 16:21 ArnabArnab 4144 silver badges11 bronze badges 4- 11 I would remend NOT nesting ternary operators. While it's always nice to have a one-liner, It's much easier for other developers to understand if you split this into if-else statements. I once had to work on an application with up to 6 nested ternaries in parts. It was a beast to debug. – JJJCoder Commented Mar 11, 2016 at 16:25
-
1
please use braces
(
and)
to differentiate expressions and make it easier for everyone's eyes. It'll most probably give you the desired result as well. – Aukhan Commented Mar 11, 2016 at 16:26 - 1 Nesting ternaries is a terrible idea.. – rlemon Commented Mar 11, 2016 at 16:26
- I am making a javascript piler. I generally dont write code like this. But piler should take anything from user if there is no syntax error. – Arnab Commented Mar 11, 2016 at 16:30
4 Answers
Reset to default 9Based on the operator precedence table:
Assignment operators has less priority than a parison operator.
So your code will be evaluated like below,
- var a = k < 100 ? o+=2 > 11 ? "T" : "F" : o < 100 ? "J" : "P";
- var a =
true
? o += 2 > 11 ? "T" : "F" : o < 100 ? "J" : "P"; - var a =
true
? o +=false
? "T" : "F" : o < 100 ? "J" : "P"; - var a =
true
? o +="F"
: o < 100 ? "J" : "P"; - var a =
true
?"78F"
: o < 100 ? "J" : "P"; - var a =
"78F"
And you can correct the behaviour by grouping the condition using a parenthesis,
var a = (k < 100) ? (o+=2) > 11 ? "T" : "F" : (o < 100) ? "J" : "P";
console.log(a); // T
var k = 27;
var o = 78;
var a = k < 100 ? (o+=2) > 11 ? "T" : "F" : o < 100 ? "J" : "P";
alert(a);
Above code works as expected by you.
You probably thought that the +=
operator would be processed first.
You are using +=
on o
instead of just +
.
var a = k < 100 ? o+2 > 11 ? "T" : "F" : o < 100 ? "J" : "P";
Also using parentheses will make it more readable:
var a = (k < 100 ? (o+2 > 11 ? "T" : "F") : (o < 100 ? "J" : "P"));
This is actually working like
k < 100 ? o += (2 > 11 ? "T" : "F") : (o < 100 ? "J" : "P");
because anything on right side of assignment operator =
is processed first, in Left to Right order, so 2 > 11 ? "T" : "F"
is evaluated first
本文标签: Javascript ternary operator resultStack Overflow
版权声明:本文标题:Javascript ternary operator result - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745263311a2650457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论