Why does var allow duplicate declaration but why does const and let not allow duplicate declaration?

前端 未结 4 605
余生分开走
余生分开走 2021-01-06 16:52

Why does var allow duplicate declaration but why does const and let not allow duplicate declaration?

var is allow duplicate declaration

4条回答
  •  爱一瞬间的悲伤
    2021-01-06 17:27

    Assuming you want to know whether this behavior is as per spec, check this 13.3.2

    Within the scope of any VariableEnvironment a common BindingIdentifier may appear in more than one VariableDeclaration but those declarations collective define only one variable.

    let and const are the recent editions, while var is probably as old as Javascript itself.

    In old days Javascript code-base didn't used to be too big to bother about programming mistakes and most probably focus was to ensure that instead of reporting the error of re-declaration of variable JS engine should handle it.

提交回复
热议问题