Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

In JavaScript, parameters of functions default to undefined. However, in some situations it might be useful to set a different default value. This is where default parameters can help.

function multiply(value1, value2=1) {
    return value1 * value2;
}
console.log(multiply(4)); // 4

The above code is equivalent to the below ES5 standard code

function multiply(value1, value2) {
    if (value2 === void 0) {
        value2 = 1;
    }
    return value1 * value2;
}
console.log(multiply(4)); // 4

Functions as parameters

Default parameters are always executed first, function declarations inside the function body evaluate afterwards.

Functions declared in the function body cannot be referred inside default parameters and throw a Error.

The below code throws ReferenceError

function multiply(a, b = sqrt()) {
    function sqrt() {
        return Math.SQRT2;
    }
    console.log(b); 
    return a * b;
}
console.log(multiply(4));

Working functions parameter code:

function sqrt() {
    return Math.SQRT2;
}
function multiply(a, b = sqrt()) {
    console.log(b); // 1.4142135623730951
    return a * b;
}
console.log(multiply(4)); // 5.656854249492381