I have a function that looks like this:
function SomeFunction() {
var SomeVar;
if (SomeCondition) {
SomeVar = 4;
}
}
This is equivalent to:
function SomeFunction() {
if (SomeCondition) {
var SomeVar = 4;
}
}
Does using the var statement only if the condition is true make a difference or not, is there any best practice or performance implications concerning this?
Thanks.
Edit: yes, I am aware that there’s no block scope, just function scope.
Those are both actually equivalent. Read up on “hoisting” and “function and block scope”.
As far as speed goes, you’re better off using the second case – combining the declaration and assignment in the same statement. See: http://jsperf.com/stackoverflow-frenchie