JavaScript Should one re-declare parameters as variables inside a function? [duplicate]

This question already has answers here:
Answer:1

There is no difference for your ECMAscript interpreter.

Both, formal paramters aswell as local variables are stored in the such called Activation Object (ES3) respectively lexical Environment Record (ES5+).

Those are, special data containers on implementation level which store data belonging to a context, like a function.

Answer:2

While there won't be any differences at runtime, most linting tools will complain about the second piece of code. JSHint gives this error:

'data' is already defined.

If you don't need the original data parameter it's safe to redeclare the variable, but usually it isn't necessary.

Answer:3

On entering function code, the identifiers in the formal parameter list are effectively treated as variable declarations, therefore including them in a var statement inside the function body has no effect.

However, there was an ancient version of Safari that threw an error in cases like:

function (foo) {
  foo = foo || {};
  ...
}

where no value was passed. However, that was a long time ago and certainly not a reason to include var statements for formal parameters.

Answer:4

If you have:

function foo(bar) {
    var bar = otherFunc(bar);
    ...
}

Although this is technically correct, I consider it poor form because you're implying that there are two distinct "bar" variables. That seems like it could cause more confusion later down the line than the version where you don't use "var".

Answer:5

I'm using Express with NodeJS and AngularJS in front-end. After click here, add data there, update some record here, show a list there, I get net::ERR_CONNECTION_RESET. It's strange, after 8 or 9 ...

I'm using Express with NodeJS and AngularJS in front-end. After click here, add data there, update some record here, show a list there, I get net::ERR_CONNECTION_RESET. It's strange, after 8 or 9 ...

I have added [?] in right side of the form to show information about respective fields. I want to show information in small box after clicking [?]. How can i do that? Thanks in advance.

I have added [?] in right side of the form to show information about respective fields. I want to show information in small box after clicking [?]. How can i do that? Thanks in advance.

How come I can replace one escaped newline \n from a text area with anything... str1 = str1.replace(/\n /g,"7 "); but it does not work with two of them? str1 = str1.replace(/\n\n /g,"7 "); ...

How come I can replace one escaped newline \n from a text area with anything... str1 = str1.replace(/\n /g,"7 "); but it does not work with two of them? str1 = str1.replace(/\n\n /g,"7 "); ...

  1. replace escaped newline

We are using a script that allows us to to change the follow up URLS of a form dynamically so we can use the same form across multiple assets but have different follow up pages. The issue is that ...

We are using a script that allows us to to change the follow up URLS of a form dynamically so we can use the same form across multiple assets but have different follow up pages. The issue is that ...