Cannot use let to define a variable with the same name of function's parameter

viery365

I am returning to javaScript after a long absence and I've just learnt about ES6 new features. And here it comes the first doubt.

Although I understand that a let variable cannot be declared twice in the same block scope I came across this situation:

    function tell(story){
       let story = {
          story
        };
     }

And it gives me a error: Uncaught SyntaxError: Identifier 'story' has already been declared

Using var this works just fine.

1) Does this mean that I will have to declare a variable with a different name of the function's parameter using let (or even const)? This can be very annoying in terms of variable naming.

In order to keep the same name I did:

function tell(story){
     story = {
        story
     };
}

2) In this case the variable story will belong exclusively to the scope of the function tell?

T.J. Crowder

Using var this works just fine.

Not really, the var is completely ignored, and you'd effectively have:

function tell(story){
   story = {
      story
   };
 }

If that's what you want, just leave the var (and let) off entirely (as above).

The error from let addresses this pitfall, where you think you've created a (new) variable but haven't. You can't use let (or const) to declare a variable that's already declared in the same scope (either as a variable, or as a parameter, which is almost the same thing as a variable).

1) Does this mean that I will have to declare a variable with a different name of the function's parameter using let (or even const)? This can be very annoying in terms of variable naming.

That's up to you. To keep doing what you were doing, just leave off var, let, and const entirely, because again, the var had no effect at all and you were assigning to the parameter, not a new variable. Or keep doing what you were doing (with var), but understand that it's misleading. Or use a different name with let or const — there are many who believe changing the value of a parameter isn't best practice (personally I'm not one of them, but...) and they'd recommend a different variable instead.

2) In this case the variable story will belong exclusively to the scope of the function tell?

Yes, you're assigning to the parameter story, which is local to tell.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

How to use a variable to define a function name?

Javascript closure (using parameter name to define an object in the same function)

Access a ES module variable from a function which use the same name in parameter

How to define an S3 generic with the same name as a primitive function?

Use Parameter Name of #define in another #define

Variable with same name inside function changes the value of the passed parameter

How come I can create a variable with same name as a function parameter?

cannot define variable in function - Go

Use a function to define an optional parameter

It is possible to use default parameter value its same function name

Cannot use type with same underlying type in function parameter

Can I use the value of a variable as a parameter name for a function?

Python3 - use function parameter as name of variable

Why can't I declare a let variable with the same name as function body in Chrome

How to use same-name global variable if passed as None to a function?

define a function use other function names as parameter

Use name of #define parameter without stringification

How it's possible to have same variable and function name in JavaScript

Function parameter name in variable in python

Python: define type of variable given into a function parameter

Why define a macro to a function with the same name?

define new variable and use it to define another variable in the same query

The variable scope in Groovy Closure (global variable and function parameter have same name)

variable and function with same name clobbering

R: variable and function of same name

what's the correct way to define a function with returned type the same as its first parameter

VBA Find Function cannot define a variable range

Ruby def function cannot define local variable

TOP Ranking

HotTag

Archive