I came across the following JavaScript in Kyle Simpson's @kyle-simpson book Functional-Light JavaScript and am curious about part of its syntax:
const double = v => v * 2;
const reducer = (list, v) => (list.push(double(v)), list);
var doubled = [1, 2, 3, 4, 5].reduce(reducer, []);
console.log(doubled);
//[2, 4, 6, 8, 10]
var reduced = reducer([3], 2);
console.log(reduced);
//[3, 6]
Specifically I am not certain about the meaning and usage of the outer parentheses surrounding the reducer function's return value:
(list.push(double(v)), list)
Wrapping a object literal in parentheses is a way to implicitly return it from an arrow function but this appears to be something different.
Also:
var val = (3, 2);
console.log(val);
//2
This seems to suggest the parentheses will return the last value in its list, which would match the signature of the reducer function, but I do not know what this construct is called.
Thanks in advance for your reply.