Javascript - A ternary statement with multiple statements

Is this valid JavaScript? I saw an example when someone used commas in ternary conditions of the operator, and it was marked as an error in my editor, and the example did not run in Chrome. However, he worked in Firefox. After I converted all ternary statements to if / else statements, the application started in Chrome.

a!==b ? (a=1, b=2) : (a=2, b=1) 

Edit:

This is the actual statement in the code:

 a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0 
+23
javascript
Jul 13 2018-11-11T00:
source share
3 answers

Yes, it does, and it works fine in Chrome:

 var a, b, c; a = 6; b = 7; c = a !== b ? (a = 1, b = 2) : (a = 2, b = 1); console.log("a = " + a); console.log("b = " + b); console.log("c = " + c); 

I am not saying that this is a remote idea in the code that people should read. :-) I expect jamietre to be right in the comments when he / she says that this is similar to the result of the evaluation.

An operator is a binary operator (an operator that takes two operands). It evaluates the left operand (thus causing any side effects, such as assignment), discards the result, then calculates its right operand (thus causing its side effects, if any) and accepts this result as its result. If you have several comma operators in a string, the general expression is evaluated in order from left to right, and the end result is the value obtained by evaluating the right-most operand.

And, of course, you know the conditional operator (the triple operator - one takes three operands) is used to select one of two subexpressions to evaluate based on the original expression.

So, this line is very ... expressive ... which contains only seven * different expressions in it.

So, in this example, the result of the general expression is 2 if a !== b initially, or 1 if a === b initially, with side effects of setting a and b .

These are side effects that make this, in my opinion, a dubious choice. And, of course, there is no reason to use a comma operator if the left operand has no side effects.




* Yes, seven of them are packaged in this common ternar:

  • a !== b
  • first expression for comma
  • a = 1
  • b = 2
  • second expression for comma
  • a = 2
  • b = 1



Repeat your editing with the actual statement, which also works:

 function test(a) { var b = 7, d = 1, e = 2, f = 3, g = 4, h = 5, i = 6; a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0; console.log("a = " + a); console.log("b = " + b); console.log("d = " + d); console.log("e = " + e); console.log("f = " + f); console.log("g = " + g); console.log("h = " + h); console.log("i = " + i); } test(0); test(1); 
 .as-console-wrapper { max-height: 100% !important; } 

But wow, I hope that this will be reduced, because if a person wrote this, they should really deal with anyone who was supposed to support him later ...; -)

+20
Jul 13 '11 at 11:56
source share

Yes:

 a=1; b=2; a!==b ? (a=1, b=2) : (a=2, b=1) console.log(a); // 1 console.log(b); // 2 

and

 a=1; b=2; a===b ? (a=1, b=2) : (a=2, b=1) console.log(a); // 2 console.log(b); // 1 

As you can analyze, changing the equality operator responds correctly to our test if you look at the results.

+18
Jul 13 '11 at 11:57
source share

Or you can do this:

 b = a!==b ? (a=1,2) : (a=2,1); 

Read about the comma here .

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

+1
Jun 26 '14 at 9:57
source share



All Articles