Some simple understanding of let, const, var

Prior to ES6, JavaScript had only two scopes: global variables and local variables within functions.


let

Variables declared with let are only valid within the code block where the let command is located.

example


const

const is used to declare one or more constants, which must be initialized when declared, and the value cannot be modified after initialization

example

If the value of const is changed


The difference between let and const and var

let

  • Block-level scope, the value can be changed, cannot have the same name as other variables or functions in its scope, and there is no variable promotion.

const

  • Block-level scope, the value cannot be changed, and cannot have the same name as other variables or functions in its scope, there is no variable promotion, and the constant declared by const must be initialized. Objects or arrays defined with const are actually mutable if they are modified.

was

  • Function-level scope, values ​​can be changed, variable promotion exists.

Guess you like

Origin blog.csdn.net/weixin_51983027/article/details/129160052