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
Add a ment  | 

4 Answers 4

Reset to default 9

Based on the operator precedence table:

Assignment operators has less priority than a parison operator.

So your code will be evaluated like below,

  1. var a = k < 100 ? o+=2 > 11 ? "T" : "F" : o < 100 ? "J" : "P";
  2. var a = true ? o += 2 > 11 ? "T" : "F" : o < 100 ? "J" : "P";
  3. var a = true ? o += false ? "T" : "F" : o < 100 ? "J" : "P";
  4. var a = true ? o += "F" : o < 100 ? "J" : "P";
  5. var a = true ? "78F" : o < 100 ? "J" : "P";
  6. 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