Is it wrong to declare a variable inside an if statement in Javascript?

前端 未结 7 710
被撕碎了的回忆
被撕碎了的回忆 2020-12-21 11:49

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{
           


        
7条回答
  •  孤城傲影
    2020-12-21 12:08

    It turns out that SublimeLinter is using JSHint which has the option to surpress this warning and explains why it exists.


    funcscope This option suppresses warnings about declaring variables inside of control structures while accessing them later from the outside. Even though JavaScript has only two real scopes—global and function—such practice leads to confusion among people new to the language and hard-to-debug bugs. This is way, by default, JSHint warns about variables that are used outside of their intended scope.

提交回复
热议问题