ilyo
ilyo

Reputation: 36421

Is there a reason to convert arguments to a local variable?

Is there a reason I shouldn't do

function bla(args){
  use(args);
}

and do this: ?

function bla(args){
  var loacalArgs = args;
  use(loacalArgs);
}

Are there places one is preferred over the other?

Upvotes: 0

Views: 62

Answers (4)

There's no need to copy your args to another variable loacalArgs unless you are doing some modifications to the original variable args.

Upvotes: 0

Gelin Luo
Gelin Luo

Reputation: 14393

One reason appear to me is that when I want to modify the argument while also want to keep an original copy. otherwise I don't think I have a reason to write additional line of code.

Upvotes: 0

SoWhat
SoWhat

Reputation: 5622

DO you mean you should copy the argument into another variable?

Its not necessary, since args itself is a local variable

Upvotes: 0

Rob W
Rob W

Reputation: 349262

There's no reason to do that, because args is alreayd a local variable.

function bla(args) {
   var localArgs = args;   //<-- This variable is as local as the other one
}

The only reason to refer to the same variable through another name is when you want to copy the reference/value, and modify one of them, eg:

function validate(word) {
    var originalWord = word;
    word = word.toLowerCase();
    if (word === originalWord) return 'At least one uppercase char is required!';
    // ... do something with both variables

Upvotes: 6

Related Questions