java-script arrow function returns (x++,x)
x
is a variable inside the outer (x =>
) function, therefore all inner functions (() => (x++, x)
) share the same variable. x++
post increments that variable whenever the inner function executes. The comma operator (..., x
) evaluates to the last comma seperated expression, x
in this case.
It is maybe easier to understand without the comma operator:
const counter = x => () => x = x + 1;