My colleague and I have been discussing good/bad practices of using the var keyword in loops. This is the my way:
for (var i = 0; i < anArray.length; i++) {
for (var j = 0; j < anotherArray.length; j++) {
var a1 = someValue1;
var a2 = someValue2;
var a3 = someValue3;
var a4 = someValue4;
var a5 = someValue5;
...... //Some other process that involve a1-5
}
}
I used var inside nested for loops. Suppose loop i
runs for 2000 times and j
for 3000 times. My colleague claim that using var in loops like these is the cause of memory leak thus, is a bad practice. Is it true?
He says that "var should be declared outside the loop so that the variables are subject to function scope and get destroyed when the scope end." Is this true?
Apart from the above claiming about memory leak, is this a bad practice, if so why?
I'm skeptical since what I know (or maybe believe) using var in loop, a1-5 still get destroyed when this function ends.