In JavaScript, there is no defined way to assign default values to function parameters that are not passed. So programmers usually check for parameters with the undefined value (as it is the default value for missing parameters) and assign the default values to them. The following example demonstrates how to do this:
function myFunction(x, y, z) { x = x === undefined ? 1 : x; y = y === undefined ? 2 : y; z = z === undefined ? 3 : z; console.log(x, y, z); //Output "6 7 3" } myFunction(6, 7);
This can be done in an easier way by providing a default value to function arguments. Here is the code that demonstrates how to do this:
function myFunction(x = 1, y = 2, z = 3) { console.log(x, y, z); }myFunction(6,7); // Outputs ...