1.var let const

A.var

1. If you define a variable using the var keyword, the variable is a local variable of the function (code block).

function test(){var a = 1; console.log(a)}
test();
console.log(a)
Copy the code

Variables defined with var are valid within the scope of the function

2. If no var keyword is used to define a variable and a global variable with the same name exists, the variable is equivalent to a global variable.

var a = 0

function test(){a = 1; console.log(a)}

console.log(a)

test();

console.log(a)
Copy the code

If we define the global definition a and assign it to 0, then code 1 will give us the global definition a, and the result will be 0

We now run the test function and assign a to 1, and step 2 agrees that a=1

Step 3 In this case, the global variable a mounted in window is still printed, and the result is 1

From this we can see that the value of a assigned to the body of test is global

3. If a variable is not initialized using the var keyword, it is undefined and local;

function test(){var a; console.log(a)} test(); // undefined

console.log(a)  // Error. The global window.a is printed
Copy the code

A defined here cannot be obtained using window.a

Var = global; var = global; var = global; var = global;

a; // Define global variables

function test(){console.log(a)} // Print a error undefined

test()

console.log(a) // Print a error undefined
Copy the code

Features: There is variable improvement

var a = 1;

(function () {

console.log(a); // undefined

a = 2;

console.log(window.a); / / 1

var a = 3;

console.log(window.a); / / 1

console.log(a); / / 3}) ();console.log(a); / / 1

console.log(window.a); / / 1
Copy the code

When a function is run, the scope of the function is formed. Var declaration will be declared earlier, so that the same variable in the local variable, so everything in the window. The value printed is the corresponding value above the global object

B.let

Features: Block-level scope ({}), no variable promotion, defined and then assigned

console.log(a); let a = 1;
Copy the code

console.log(a); var a = 1;
Copy the code

for(var i = 1; i < 5; i++){

    setTimeout(() = > console.log(i), 0)}Copy the code

Here I is overridden each time by an incremental value, equivalent to a global variable, and setTimeout is executed asynchronously, so it’s all 5

for(let i = 1; i < 5; i++){

    setTimeout(() = > console.log(i), 0)}Copy the code

  1. for( let i = 1; i< 5; I++) has a hidden scope between the parentheses
  2. for( let i = 1; i< 5; I ++) {loop body} Before each execution of the loop body, the JS engine will redeclare and initialize I in the context of the loop body.

The equivalent of

{let i = 1}

{let i = 2}

{let i = 3}

{let i = 4}
Copy the code

c.const

Definition: Constants are block-scoped, much like variables defined with let statements. But the value of a constant cannot be changed (by reassigning), nor can it be redeclared.

Different from var. Open objects defined as const do not hang in the window and must be initialized with assignment. When a const value is of a basic type, the value cannot be changed. When a value is an array object, the key and value can be changed but the entire object cannot be overwritten

const FLAG = true

let FLAG = false

var FLAG = false
Copy the code

const A = [1.2.3.4]

A = [1] // error

A.push(5) // A = [1, 2, 3, 4, 5]
Copy the code

In the future, ES6 and the latest proposal will be sorted out in the form of notes