I just came across some notation in JavaScript like so:
var a = (1,2,3,4,5);
This will always return the last value, in the above case 5. I’m aware of using brackets to namespace my JavaScript code, but have never seen it used this way.
Is there any use for this notation, or is it just some JavaScript byproduct?
It’s the comma operator. As the mdn states (link) it always returns the later value. In your example it doesn’t make much sense, since it will always assign
a = 5. But consider this:It’s used to increment and decrement in a single statement:
i++, j--Edit:
The parentheses in your example are necessary because its a variable declaration. In other cases they can be left out.