Skip to content Skip to sidebar Skip to footer

Scope Of Variables Inside Inline Function Default Parameter

ES6 introduced default parameters. I'm trying to understand how inline function default parameters work with this new feature. Specifically how its scoping work. Take for example t

Solution 1:

You are correct. In both of the top functions x in f refers to the parameter x.

There's some considerations with case 3.

In the third example if f isn't defined and you are returning x2 when calling the function it will be equal to whatever x originally was. When you do x = 5; you aren't changing x2. This is because when you assign x2 = x JavaScript makes a copy not a reference.

Unless the x parameter is passed an array or objectx2 will be a copy and not a reference of x.

So if you do three(3) then x2 will always be 3 because you're never changing it.

Post a Comment for "Scope Of Variables Inside Inline Function Default Parameter"