The difference between let, const, and var

let const is a new method of declaring variables in ES6, which is used to solve some problems of var declaration

Var has variable promotion. Let and const
can be repeated without var. Let and const do not.
Var does not have block-level scope, which will cause variable penetration. Let and const have block-level scope (both for and if). Will cause
let through , const will cause temporary dead zone

var tmp = 123;
if (true) {
    
    
  tmp = 'abc'; // ReferenceError
  let tmp;
}
//以上代码if后面{}形成了块级作用域,由于使用let声明了tmp,则这个变量就绑定了块区域,在声明之前使用,会报错。

Guess you like

Origin blog.csdn.net/weixin_51198863/article/details/113611959