0
JS
var a = (1,5 - 1) * 2; alert(a); // why result is 8 ???
5 Antworten
+ 2
the JS comma operator is similar to the C comma operator. It evaluates the expression on its left side, then evaluates the expression on its right side, and returns this value.
In your case, "1" is evaluated first, and its result is then discarded, then "5-1" is evaluated, then multiplied by 2.
This operator is useful when the expression on the left side has side effects, for example :
while(j *=2, ++i < 10);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
+ 1
you probably should use . instead of , so it should look like this. var a = (1.5 - 1)*2
0
it is not mistake. in question used ","
0
and I do not understend logic of this expression (((
0
Udi thank you )