I have a Sublimelinter installed in Sublime Text 2 and it\'s great. However it doesn\'t like the following code:
if(condition){
var result = 1;
}else{
Javascript doesn't have 'block-scoping' like many other languages. If it did, the variable result
would not exist when you tried to call process(result)
because it would not be possible to reference it outside of the {}
block where it was defined.
However, javascript only has function scoping, where variables in one function cannot be accessed by another function. Where variables are declared in the function has no significance whatsoever, because it will still be accessible from anywhere inside that function (no block scope). Hence, both code snippets you posted are equivalence to whatever interpreter is running the code.
The second is preferable because it is clearer as it shows where the variable will be used (throughout the function). It also prevents the variable from being declared twice inside the scope of the function, which may not necessarily cause anything bad to happen, but it will definitely not cause anything beneficial. You should almost always declare a variable once at a higher level instead of twice at different lower levels, even though it doesn't really matter since the scope of the variable will be the entire function no matter where it is declared.