I am aware that most common practice would be to put var a,b; on the top, but I want to extract every possible character (after running on JS Uglify), and it seems they don’t delete unnecessary var initializing
I want to know if any of the following will cause problems and which is recommended
Case 1:
if(condition){
var a=-1;
var b="++";
}else{
var a=1;
var b="--";
}
Case 2:
if(condition){
var a=-1;
var b="++";
}else{
a=1;
b="--";
}
Case 3:
if(condition){
a=-1;
b="++";
}else{
var a=1;
var b="--";
}
It doesn’t matter, since JavaScript has function scope, not lexical scope.
You can think of it as every
var ...;statement being shunted up to the top of the function they’re in. (That’s what I do, at least.)I’d write the code as