The difference between let, const and var in ES6

1. The scope of variables defined by var is the entire closed function, which is global; the scope of variables defined by let is at the block level or in the block;

Second, variable promotion: no matter which line the variable declared by var is in the current line, it will be promoted to the top of the scope.

  而let声明的变量不会在顶部初始化,凡是在let声明之前使用该变量都会报错(引用错误ReferenceError);

Third, as long as there is a let in the block-level scope, the variables it declares will be bound in this area;

Fourth, let is not allowed to be declared repeatedly in the same scope (using var and let at the same time when an error is reported, two let).

const is used to specifically declare a constant. It acts on the block-level scope like let. There is no variable promotion. Repeated declarations will report an error. The difference is that the constant declared by const cannot be changed and must be initialized (assigned) during declaration.

Guess you like

Origin blog.csdn.net/weixin_45663264/article/details/107765031