How are functions with default arguments created?
How are functions with default arguments created?
explanation
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn’t provide a value for the argument. In case any value is passed, the default value is overridden.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
JavaScript Demo: Functions Default
function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(5, 2));
// expected output: 10
console.log(multiply(5));
// expected output: 5
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
Step by step
Solved in 2 steps