A default parameter can make a function behave as though its body had an extra scope. JavaScript evaluates default expressions in an intermediate parameter scope between the outer scope and the function body. section 6.4
The scope rules explain several non-obvious results:
- A default expression resolves names from the outer scope, not from declarations inside the function body. section 6.4
- Earlier parameters are visible to later default expressions, so b = a can reference a. section 6.4
- A body declaration can shadow a parameter without changing the value captured by a function created in the parameter scope. section 6.4
const x = 'outer';
function example(a = x, b = a) {
const x = 'inner';
console.log(a, b);
}
example(); // 'outer', 'outer'Compare outer-scope lookup with a same-named declaration inside the function body. section 6.4
function strange(a = () => b, b = 1) {
let b = 2;
return a();
}
strange(); // 1, not 2!Observe a closure capturing the parameter-scope binding rather than the body’s shadowing declaration. section 6.4


