Defining JavaScript variables inside if-statements

Viewed 55351

Is defining JavaScript variables inside if-statements correct?

if(a==1){
    var b = 1;
} else {
    var b = 0;
}

I know the code above will work, however, WebMatrix highlights the variables.

Should I define the variables outside the if-statement? Or the first option's correct? Or it doesn't really matter?

var b = '';
if(a==1){
    b = 1;
} else {
    b = 0;
}
5 Answers
Related