Consider this:
var i = 0;
var isEight = false;
while(true){
if( i === 8) {
isEight = true;
break;
}
i++;
}
versus this:
var i = 0;
while(true){
var isEight = false; // moved declaration inside
if( i === 8) {
isEight = true;
break;
}
i++;
}
The example is contrived but you get the idea. How does Javascript respond when the declaration of isEight
is brought inside the loop?
Does a new memory space get allocated every time that line runs? Will this be a serious issue if the loop runs many many times?
Update: Since alex pointed out, what about using let
instead of var
in this case.