I know that in JS point is a decimal mark and I see that there's a comma between 1
and 5
, but why does (1,5 - 1)
equal to 4
and what happens with 1,
in the beginning of the state
Asked
Active
Viewed 45 times
0

Artem Kuroptev
- 29
- 1
- 3
-
`1,5` is not one number. It's actually three things: the number `1`, a comma and the number `5`. So: `(1,5 - 1)` = `(1, 5 - 1)` = `(1, 4)` and the `1` is thrown away so the result of this expression is `4`. – Jesper Apr 26 '22 at 08:55
-
[JavaScript Comma Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) – Usama Apr 26 '22 at 08:56
-
I don't understand why you don't use 1.5 but use 1,5 – Minh Viet Apr 26 '22 at 09:00
-
1@MinhViet: Some languages use `,` as decimal separator. OP was probably curious how/why the expression still produced a result. – Felix Kling Apr 26 '22 at 09:07
-
1@MinhViet, I was solving some js-knowledge tests when i got the task. – Artem Kuroptev Apr 26 '22 at 11:21
-
@Jesper, That's exactly what I described in question. I want to know why `1` is thrown away from `(1, 4)`. I mean it is obvious that `(1, 5 - 1)` equal to `(1, 4)`. The point is why `(1, 4)` equals to `4` – Artem Kuroptev Apr 26 '22 at 11:22
-
It is the [comma operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator). By itself it's not useful, but it is useful, for example, to initialize multiple variables in a `for` statement. – Jesper Apr 26 '22 at 13:11