How about using the spread operator for handling optional arguments, or for null arguments?
Consider this code -
|
|
We can choose to pass ‘z’ or not, but ignoring ‘y’ returns an error.
|
|
Sure, you can use a null
or undefined
in the middle and get away with it. But, you could also replace that with the spread operator like so -
|
|
You still get a NaN
, but no run time errors!
We use spread operator to destructure the array with zero or more elements, and pass it to the said function. While it is minimal overhead work at the caller end, the signature of the called function will remain intact.
Although the use of spread operator like the above is interesting, consider using named arguments within a single object in your functions. Also, see rest parameters.