Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If I have a number of functions that take json object parameters, does it make any difference whether I assign them to a variable before using them inside the function:

Function doSomething(data){
var abc = data;

abc.filter….etc.

}

Vs.

Function doSomething(data){

Data.filter….etc

}

is one way better than the other?

share|improve this question
In your second code snipped, your parameter is "data" (lower-case "d") but the code references "Data" (upper-case "d"). JavaScript is case-sensitive. (Well also of course you used "Function" ...) – Pointy Nov 26 '12 at 22:48
1  
It is completely redundant to do that. – Esailija Nov 26 '12 at 22:48
Pointy - it was a typo but thanks for noting that – user1783490 Nov 26 '12 at 23:11

3 Answers

up vote 1 down vote accepted

This makes no difference and it your example, the new variable is redundant. It is good practice not to create extra variables. It might be useful to do this if your JSON is heavily nested.

data = { foo: { bar: { baz: [] } } }

function doSomething(data) {
  var innerData = data.bar.baz;
  for(var i=0; i<innerData.length; i+) {
    // Whatever.
  }     
}

This will save you having to reference data.foo.bar.baz all the time.

share|improve this answer

It is completely redundant to create the abc variable in the first example.

Consider how it's really evaluated:

function doSomething() {
    var data = arguments[0];
    var abc = data; //why?
}
share|improve this answer

Yes, it is better not to create the useless extra variable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.