This is the 14th day of my participation in the August Text Challenge.More challenges in August

scope

The variable declared by var is the function scope

Variables declared by let and const are block-level scopes

Variable ascension

Variables declared by var are promoted (promoted to the top of the current scope), and promoted by function. That is, a variable can be called before it is declared, with the value undefined.

Let and const do not have variable promotion. That is, the variables they declare must be used after the declaration, otherwise ReferenceError is reported.

Temporary dead zone

Let and const** have temporary dead zones. That is, as long as the let command exists in the block-level scope, the variables declared by it are “binding” to the region, no longer subject to external influence.

``` var tmp = 123; if (true) { tmp = 'abc'; // ReferenceError let tmp; } ` ` `Copy the code

If {} is a block-level scope. TMP is a block-level scope. If {} is a block-level scope, TMP is a block-level scope.

Global declarations

Variables declared globally by var become properties of the window object

Variables declared by let and const globals do not become properties of the window object

```
var name = 'Johnson';
window.name === name // true
​
let job = 'frontend';
window.job === job // false
```
Copy the code

Duplicate declaration of variables

Var allows variables to be declared repeatedly

Let and const are not allowed to declare variables twice in the same scope

Variables are covered

Var declares variables, function scopes, and can override previously declared values

Var price = 100 var count = 10 if (count > 5) {var discount = price * 0.6 console.log(' the discount is ${discount}`) } ```Copy the code

Let, const declaration variable, block scoped, cannot overwrite the previously declared value

Var price = 100 var count = 10 if (count > 5) {let discount = price * 0.6 console.log(' the discount is ${discount}`) } ```Copy the code

Conditional declarations (if, try/catch)

Var can declare variables using conditions

Let and const using conditions to declare variables are invalid

Whether declared variables can be modified

Var and let can declare a read-only constant. Must be initialized immediately when declared.

When const declares a reference to a variable of type, the name of the variable does not point to the data, but to the address of the data.

Let and const declarations in the for loop

let

The part of the for loop that sets variables is a parent scope, while the inside of the loop body is a separate child scope.

```
for (let i = 0; i < 5; i++) {
    console.log(i)
}
```
Copy the code

1. First, the scope of the variable _ I is only valid in (). The body of the loop cannot access _ I

2. Create an I variable each time through the loop, assigning the _i in parentheses to variable I 3. And then I ++ and then I will assign the value of I back to _i

const

A const cannot be used to declare an iteration variable because it increments.

You can declare for loop variables that will not be modified using const; each iteration just creates a new variable.

``` for (const key in {a:1,b:2}) { console.log(key); } //a,b for (const value of [1,2,3,4,5]) {console.log(value); } ` ` `Copy the code

Improve performance with lets and const

In cases where the block scope terminates earlier than the function scope, these two new keywords may allow the garbage collector to step in earlier to reclaim the memory that should be reclaimed earlier than the function-scoped VAR, block-scoped let and const.