# JS Tip of the Day: Default Parameter Scope

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-default-parameter-scope/643536>\
**Category:** web dev\
**Created:** [July 12, 2020, 3:52pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-default-parameter-scope/643536 "2020-07-12T15:52:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 12, 2020, 3:52pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-default-parameter-scope/643536/1 "2020-07-12T15:52:02Z")

</div>

**Default Parameter Scope**  
Version ES2015  
Level: Intermediate

Normally, when function parameters are initialized in a function call, they’re scoped to the function body. However, when functions use default parameters, the parameters are given an additional, separate scope to be initialized in.

A function like this:

```javascript
function paramScopes (param) {
    let local = 2;
}

```

Can be seen as something like the following:

```javascript
function paramScopes () {
    let param = arguments[0];
    let local = 2;
}

```

But given (note the added parameter default):

```javascript
function paramScopes (param = 1) {
    let local = 2;
}

```

What you get translates to something a little more like:

```javascript
function paramScopes () {
    let param = arguments[0] !== undefined ? arguments[0]: 1;
    {
        let local = 2;
    }
}

```

This separation of parameters with local function variables allows parameters to refer to variables in the outer scope that may have a similarly named variable in the function body.

```javascript
let value = 'outer';
function paramScopes (param = value) {
    let value = 'inner';
    console.log(param);
}
paramScopes(); // outer

```

If the `param` parameter above would have been in the same scope as the rest of the function body, it would have referred to the inner value rather than the outer, and as a result would likely resolve to `undefined` or throw an error due to the effects of hoisting.

More info:

- [Default parameters on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters)

* * *

More tips: [JavaScript Tips of the Day](http://forum.kirupa.com/t/javascript-tip-of-the-day/641803/1)

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 12, 2020, 6:25pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-default-parameter-scope/643536/2 "2020-07-12T18:25:31Z")

</div>

Sort of reminds me of elaborations in Ada:

[http://www.cs.uni.edu/~mccormic/4740/documentation/elaboration](http://www.cs.uni.edu/~mccormic/4740/documentation/elaboration)
