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

前端 未结 7 692
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  萌比男神i
    2020-12-21 12:16

    It is not wrong. If you get that error, you defined result earlier in your code.

    You can also simplify your condition to this so you don't have to use result:

    process( condition ? 1 : 2 );
    

提交回复
热议问题