This is the 9th day of my participation in the August Wen Challenge.More challenges in August
preface
As we all know, before ES6, we used var to declare variables.
Es6 has added two new commands to declare variables, const and let.
The let command
The let command declares variables, which can only be accessed and used in the current scope without variable promotion (variable promotion is covered below).
let name = 'the answer cp3'
console.log(name) / / the answer cp3
Copy the code
Const command
The const command declares a constant that cannot be changed or promoted.
const name = 'the answer cp3'
console.log(name) / / the answer cp3
name = 'cp3' Assignment to constant variable
Copy the code
While const declares a constant to be immutable, it only guarantees that the value of the variable on the stack is immutable.
- So if you declare variables as primitive types, that’s fine, because their values are stored on the stack and are guaranteed to be immutable
- But if the variable is a reference type, because the value of the reference type is stored in the heap, the stack only stores the address of the value pointing to the heap, and the value of the heap can change
const obj = {}
obj = {}
console.log(obj) Assignment to constant variable
const obj = {}
obj.name = 'the answer cp3'
console.log(obj) // {name: "answer cp3"}
Copy the code
Here’s how they differ from VAR.
-
Var boosts variables, const and let do not. Variable promotion refers to declaring a variable as undefined at the beginning of the current scope.
console.log(name) // undefined var name = 'the answer cp3' Copy the code
console.log(name) // name is not defined let name = 'the answer cp3' Copy the code
Var declares name, which will promote the variable, declare the variable as undefined, and then assign the answer cp3
Is equivalent to
var name = undefined console.log(name) // undefined name = 'the answer cp3' Copy the code
Const and let are not variable promoted and cannot be used before declaration, which is grammatically called temporal dead zone (TDZ).
-
Var can be declared repeatedly. Const and let cannot be declared repeatedly in the same scope.
var name = The 'with answers' var name = 'cp3' / / normal let name = The 'with answers' let name = 'cp3' // Warning Identifier 'name' has already been declared const name = The 'with answers' const name = 'cp3' // Warning Identifier 'name' has already been declared Copy the code
If it is declared under a different scope, it is fine, as follows
let name = The 'with answers' if(true) { let name = 'cp3' / / without error } Copy the code
Note: Because the if statement belongs to another block-level scope, it does not affect it
-
Var declarations in the global scope mount variables on window objects, but let and const declarations do not mount variables on any objects.
var name = 'the answer cp3' cosole.log(window.name) / / the answer cp3 let name1 = 'the answer cp3' console.log(window.name1) //undefined const name2 = 'the answer cp3' console.log(window.name2) //undefined Copy the code
conclusion
To summarize the const and let commands:
- Const declares a constant, which guarantees immutable values for base types but not for reference types
- Let declares variables. The same variable cannot be declared repeatedly in the same scope, nor is const
- Const and let do not have variable promotion
- Declarations of const and let in global scope are not mounted to
window
object
Thank you for reading.