Defining JavaScript variables inside if-statements

前端 未结 5 1135
悲&欢浪女
悲&欢浪女 2021-02-01 00:57

Is defining JavaScript variables inside if-statements correct?

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

I know the code above will wo

5条回答
  •  臣服心动
    2021-02-01 01:40

    Note that ECMAscript 6 does support block-level variables using the 'let' rather than the 'var' keyword. While variables declared with 'var' are hoisted to be function-scope regardless of where they are declared, those defined using 'let' are scoped to the enclosing block only.

提交回复
热议问题