Reputation: 8698
I don't see that in any shorthand technique or stackOverflow questions..then I wonder if the following can be a shorthand technique.
Imagine I have a function which I know there is exactly 1 argument that will be passed to it :
function myFunc(arr) {
var i;
for(i = 0; i < arr.length; i++ ) {
if(arr[i] === 3) {return true;}
}
}
Is it a good practice in that case to write :
function myFunc(arr, i) {
for(i = 0; i < arr.length; i++ ) {
if(arr[i] === 3) {return true;}
}
}
I know that in most case we only save 4 bytes and this represent a very small improvement but sometimes for a short function it can be more readable without wasting 1/2 lines just to declare variables.
Edit: also I want to declare i in the scope of the function and not in the for
loop sinc I want to be able to reuse it.
Upvotes: 0
Views: 58
Reputation: 6780
You would only do this if you were going to actually use i
in the for
loop.
e.g:
function myFunc(arr, i) {
for(i; i < arr.length; i++ ) {
arr[i];
}
}
// Why you would do this though, is another matter (and bizarre)
myFunc(anArray, 9);
Instead, it would be better to do:
function myFunc(arr) {
for(var i = 0; i < arr.length; i++ ) {
arr[i];
}
}
and not worry about 4 bytes...
Upvotes: 1