The syntax to set the default parameter value for a function is:
function functionName(param1=default1, param2=default2, ...) {
// function body
}
Example 1: Set Default Parameter Value For a Function
// program to set default parameter value
function sum(x = 3, y = 5) {
// return sum
return x + y;
}
console.log(sum(5, 15));
console.log(sum(7));
console.log(sum());
Output
20 12 8
In the above example, the default value of x
is 3 and the default value of y
is 5.
sum(5, 15)
- When both arguments are passed,x
takes 5 andy
takes 15.sum(7)
- When 7 is passed to thesum()
function,x
takes 7 andy
takes the default value 5.sum()
- When no argument is passed to thesum()
function,x
takes the default value 3 andy
takes the default value 5.
Example 2: Using Previous Parameter in Another Parameter
// using previous parameter in default value expression
let calculate = function(x = 15, y = x + 2) {
return x + y;
}
const result1 = calculate(10);
console.log(result1);
const result2 = calculate();
console.log(result2);
Output
22 32
You can also pass a parameter as the default value for another parameter.
In the above program,
- When 10 is passed to the
calculate()
function,x
becomes 10, andy
becomes 12 (thesum
function returns 22). - When no value is passed to the
calculate()
function,x
becomes 15, andy
becomes 17 (thesum
function returns 32).