0

JS

var a = (1,5 - 1) * 2; alert(a); // why result is 8 ???

3rd Nov 2016, 6:21 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar
5 odpowiedzi
+ 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
3rd Nov 2016, 6:54 PM
Udi Finkelstein
Udi Finkelstein - avatar
+ 1
you probably should use . instead of , so it should look like this. var a = (1.5 - 1)*2
3rd Nov 2016, 6:47 PM
Marcin Hędrzak
Marcin Hędrzak - avatar
0
it is not mistake. in question used ","
3rd Nov 2016, 6:51 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar
0
and I do not understend logic of this expression (((
3rd Nov 2016, 6:53 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar
0
Udi thank you )
3rd Nov 2016, 7:18 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar