2

Consider:

var success = function(data){
    data = process_data(data);
    console.log(data);
};

vs.

var success = function(data){
    var data = process_data(data);
    console.log(data);
};

What are the pros/cons of each?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
cammil
  • 9,499
  • 15
  • 55
  • 89
  • there will be no difference besides saving 4 characters with the first version. – Dennis Jul 26 '13 at 11:15
  • The argument is already a local variable, so you don't have to declare a replacement local variable. so go with the first. – AngelWarrior Jul 26 '13 at 11:18
  • Related http://stackoverflow.com/a/1864059/133198 A function's parameter is defined as a variable and hence declaring `var data` is redundant and not needed. – vsr Jul 26 '13 at 11:48

5 Answers5

3

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.

jAndy
  • 231,737
  • 57
  • 305
  • 359
1

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.

Sacha
  • 2,813
  • 3
  • 22
  • 27
1

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.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • What problem did Safari have with that code? – Bergi Jul 26 '13 at 11:24
  • I think it threw a reference error. It was a problem with functions intended as listeners with the classic `evt = evt || window.event`. Simple to fix with `var evt = ...`. But no one does that now, though some code from that era (2004?) may include it. – RobG Jul 26 '13 at 11:43
1

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".

Soron
  • 448
  • 3
  • 10
0

If you want to declare a variable inside then probabley you should declare it with some other name(not similar to the argument).

This way you will be able to reuse the original argument.

If you don't want to use the original argument then you can directly use it. But remember declaring a new variable inside will restrict the scope of the variable.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83