1. Let and const do not have variable promotions and cause temporary dead zones

What is variable promotion?

Var variable promotion before ES5:

console.log(foo); //undefined

var foo=2;
Copy the code

After the let

console.log(foo); //ReferenceError

let foo=2;
Copy the code

What is a temporary dead zone? Within a code block, a variable is not available until it is declared using the let command, which is syntactically called a temporal dead zone.

tmp='abc' //ReferenceError
console.log(tmp) //ReferenceError

let tmp;
console.log(tmp) //undefined

console.log(tmp) //123
Copy the code

The essence of a temporary dead zone is that once you enter the current scope, the variable you want to use already exists but cannot be retrieved until the line of code that declared the variable appears.

2. Block-level scope solves the problem of variable promotion

var test=aaa;
function f(){
        var test = '111'}} f() //undefined variable promotion causes the innertestThe variable overrides the outertestvariableCopy the code

Another common one is the I for the for loop

var s='hiabu'
for(var i=0; i<s.length; i++){ ... } console.log(I) // 5i only controls the loop, but after the loop ends, it does not disappear, but leaks into global variablesCopy the code

Block-level scope:

let n=5;
    let n=10;
console.log(n) //5
Copy the code

ES6 specifies that: * allows block-level function declarations * function declarations like var are promoted to the head of the global scope or function scope * function declarations are promoted to the head of the block-level scope

function f(){console.log('i am out')}
(function() {if(false) {function f(){console.log('i am in')}
    f(); //f is not a function} ())Copy the code

If you do need a declaration, use the form of a function expression, not a function statement

let f =function(){} // expressionfunction f() {} / / statementsCopy the code

In addition, the block-level scope does not contain a return value, so you can use a DO expression if you need a return value

let x=do{
    let t =f();
Copy the code

4. Const returns an error if it is declared without assigning

const foo; / / complainsCopy the code

Const guarantees that the value will not change. In fact, the memory address that the variable points to will not change. For simple data, the value is stored at the memory address that the variable points to and is therefore equivalent to a constant. In the case of a compound type, the pointer to the memory address holds a pointer, and const only ensures that the pointer is fixed

const a =[];
a.push('hi'); // execute a.length=0; // a=['aaa'] // An error will be reportedCopy the code

If you want to freeze an Object, you can use the object. freeze method to freeze the Object completely as follows:

var constant = (obj) =>{
        if(typeof obj[key]==='object'){ constant(obj[key]); }})}Copy the code

5. Starting from ES6, global variables are isolated from top-level object attributes

var a=1;
window.a  //1

let b=1;
window.b  //undefined
Copy the code

Gets the top-level object method

var getGlobal=function() {if(typeof self ! = ='undefined') {returnself; }if(typeof window ! = ='undefined') {returnwindow; }if(typeof global ! = ='undefined') {returnglobal; } throw new Error('unable to locate global object')}Copy the code