What are the differences between let, const and var?

Posted by sentient0169 on Thu, 03 Oct 2019 10:29:48 +0200

1.var was proposed in ES5, let and const in ES6.

2. When defining variables, var has variable hints, let and const do not have variable improvements.

var a = 100;
consoloe.log(a);//100

consoloe.log(a);//underfinde proves that a has been declared at this time, but not assigned
var a = 100;

consoloe.log(b);//ReferenceError: b is not defined cannot find the variable b
let b = 100;

consoloe.log(c);//ReferenceError: c is not defined cannot find the variable c
const c = 100;

3. Variables declared by VaR are mounted on window s objects, while variables declared by let and const are not.

var a = 100;
console.log(a,window.a); //100 100

let b = 100;
console.log(b,window.b); //100 undefined

const c = 100;
console.log(c,window.c);//100 undefined

4. Variables declared by let and const form modular scopes

{
   var a = 100;
   let b =100;
}
cosnole.log(a);//100
cosnole.log(b);//ReferenceError: b is not defined cannot find the variable b
{
   var a = 100;
   cosnt b =100;
}
cosnole.log(a);//100
cosnole.log(b);//ReferenceError: b is not defined cannot find the variable b

5. In the same module scope, let and const cannot declare variables with the same name, while var overrides the contents of variables with the same variable name later.

var a = 100;
var a = 10;
console.log(a);//10

let b = 100;
let b = 10;
console.log(b);//Identifier'b'has been declared already 

const c = 100;
const c = 10;
console.log(c);//Identifier'c'has already been declared B has been declared 

6. Both let and const have temporary dead zones, i.e. only let and const commands declare variables in the module area, and the variables declared by them are bound to this area and are not affected externally.

var a = 100;

if(true){
    a = 10;
    //In the case of a using let/const declaration in the current block scope, when a is assigned 10, only variable a is found in the current scope.
    // At this point, it's not time to declare, so the console Error:a is not defined
    let a = 1;
}

Within the code block, variables are unavailable until they are declared using the let command. This is grammatically known as "temporal dead zone" (TDZ).
In short, the essence of the temporary dead zone is that once you enter the current scope, the variable you want to use already exists, but it can not be obtained. Only when the line of code declaring the variable appears, can you get and use the variable.

7. Variables declared by VaR and let are modifiable. const declares a constant. Once declared, the value of the constant cannot be changed.

const f = 10;
// f= 11;
// console.log(f)//Error cannot be repeated

const obj = {
    name: 'Xiao Ming',
    age: 18
}
obj.age = 20
console.log(obj) //{name:'Xiaoming', age: 20}  
//const declares constants and does not allow reassignment of variables. For reference type values, as long as the address value stored in stack memory remains unchanged.

This paper refers to Ruan Yifeng's ES6.