Recently i found myself attaching function arguments to a variable inside the function scope so that i was not referencing the argument every time it was used.
Is there any benefit to this practice?
For example:
function populateResultCount(count){
var count = count;
return $('.resultCounter').text(count);
};
Could easily be re-written like so:
function populateResultCount(count){
return $('.resultCounter').text(count);
};
And would still function correctly.
If you’re not using the argument that’s passed in, there is no difference. In your first example, you can potentially confuse future maintainers because of
var count = count, i.e., you’re declaring a variable that has the same name as the argument, and that isn’t a best practise.So, if you can, use your second form. Its intent is clearer and there is no room for confusion.