Arguments are a mere suggestion to call a function in JavaScript. If you supply more arguments than required, they are simply ignored. If you supply less, the missing ones are set to undefined
.
To handle the case of fewer arguments than the function specified, you can either check for undefined
or provide default arguments.
Both of these work:
const avg = (x, y) => y === undefined ? x : (x + y) / 2;
const avg = (x, y = x) => (x + y) / 2;
I prefer the latter but your mileage may vary.