If a function is called with fewer arguments than declared, the missing values are set to undefined.
While this may be acceptable in some cases, it’s often better to assign default values to parameters.
function myFunction(x, y) { if (y === undefined) { y = 2; } } |
ES6 allows function parameters to be assigned default values.
If y is not provided or is undefined, then y will default to 10.
function myFunction(x, y = 10) { return x + y; } myFunction(5); |