2

Here in the ECMAScript docs it details FunctionDeclarationInstantiation and on line 28 it details how the scope is created, although line 30 also seems to show a scope could be created, but it seems this is the correct line.

In NOTE 1, it also states:

If default value parameter initializers exist, a second Environment Record is created for the body declarations

A separate scope is created for default parameters:

function sum(a = 1, b = () => a) {
  return a + b()
}

But I have a few questions,

1.) Are the parameters initialized as var declarations or let declarations or neither?

2.) Why is the second code snippet creating a block for const c = 3 whereas in the first snippet only an expected local scope is created?

here const c = 3 is in local scope

function sum(a, b) {
  debugger
  const c = 3
  return a + b
}

here const c = 3 is in a block scope

function sum(a = 1, b = 2) {
  debugger
  const c = 3
  return a + b
}

3.) If a new scope is created, why does the first code snippet work as expected by having two var a variables, but in the second snippet, the compiler complains there is a re-declaration?

function sum(a = 1, b = 2) {
  debugger
  var a = 3
  return a + b
}

I would expect to have two const a variables here as well.

function sum(a = 1, b = 2) {
  debugger
  const a = 3
  return a + b
}
dbzx10299
  • 722
  • 2
  • 14
  • “ is the spec trying to say they are initialized as var variables?” — like… globals? – Dave Newton Jun 19 '23 at 17:44
  • 1
    Steps 30 and 31 say what to do in non-strict and strict mode, respectively. They both depend on the `varEnv` produced in step 28. – Barmar Jun 19 '23 at 17:47
  • Read **Note 1** at the beginning of this section. When there are default values, they're evaluated in a scope that contains preceding parameters. But the body scope is separate from this. – Barmar Jun 19 '23 at 17:57
  • What is causing `const c = 3` to be block scoped if a default param is present? I know a seperate scope is created for default params but I still am not quite clear why this is causing block scoping in the function body – dbzx10299 Jun 19 '23 at 18:04
  • 1
    Does this answer your question? https://stackoverflow.com/questions/44896829/scope-of-default-function-parameters-in-javascript/44896990#44896990 – Bergi Jun 19 '23 at 21:18
  • @Bergi I am still a bit confused how the scope that is created for the default variables causes a block scope to be formed in the function body for `const c ...`. I am clear default params form a new scope, I am still not quite understanding how this makes a block scoped variable in the function body that would otherwise be in a function (local) scope. – dbzx10299 Jun 19 '23 at 21:24
  • @Bergi I have thoroughly read through your examples but in my example, if `const c = 3` is replaced with `var c = 3` still the dev tools show a block scope created for `var c = 3`. Is this just for DX, I know that the dev tools are more of an emulator and I know that `var` will **not** be block scoped even though `function (a = 1) { var x = 1 }` shows that var is block scoped in the dev tools – dbzx10299 Jun 20 '23 at 16:04
  • 1
    Probably a DX thing with the devtools not following the spec language. It actually **is** an extra nested scope in which the `var` gets placed - can we call it a "block" scope though if there is no block? Notice what happens if you replace the `const c` with `var a` - you actually get two separate variables `a` (though the first one is only accessible to code in closures in the parameter defaults, like in your first snippet). – Bergi Jun 20 '23 at 16:27
  • @Bergi, I did notice that and that is contributing to my confusion, if there are two separate `var a` variables why can't we use `const a` in place of `const c`? I would also expect there to be two separate variables but the compiler complains there is a re declaration – dbzx10299 Jun 20 '23 at 19:02
  • @dbzx10299 `var` has the legacy semantics that duplicate declarations must be allowed. They probably could have forbidden that when introducing the separate scope for non-simple parameter lists. Not sure if it was overlooked or deemed not important enough. – Bergi Jun 20 '23 at 19:50

0 Answers0