I recently discovered that (1,2)
equals 2
Same for (1,3,4) => 4
always return the last number
What is this expression exactly? It's not an array, not an object, what is it ?
I recently discovered that (1,2)
equals 2
Same for (1,3,4) => 4
always return the last number
What is this expression exactly? It's not an array, not an object, what is it ?
Code within parentheses is evaluated as an expression. Several expressions can be grouped together by using a comma
. Commas are not just syntax for dividing arguments or function parameters, they double as an operator
- much like +
, -
, etc serve multiple purposes, so does ,
In the case you've outlined (1,2)
the grouped expressions are obviously 1
and 2
. They are both evaluated to themselves, but when expressions are grouped by comma operators, only the last evaluated expression is returned.
This means:
(1,2) == 2
and:
(1,2,3,4) == 4