Var const let = var const let


<font color=#999AAA >

Var declaration

<font color=#999AAA > <font color=#999AAA >

var msg;
console.log(msg)  //undefined

<font color=#999AAA >2. <font color=#999AAA >2. <font color=#999AAA >2

function test(){ var msg = 'Bear'; } test(); console.log(msg); / / an error

<font color=#999AAA > <font color=#999AAA > <font color=#999AAA > < / font > < / font

function test(){ console.log(name); var name = 'Bear'; } function test(){var name;} function test(){var name; console.log(name); name = 'Bear'; } test() //undefined

2. Let declaration

  1. Let is similar to var, except that the scope of a let declaration is a block scope, while the scope of a var declaration is a function scope
if(true){ var name = 'bear'; console.log(name); //bear } console.log(name); //bear if(true){ let name = 'bear'; console.log(name); //bear } console.log(name); // ReferebceError:name is not defined

2. Let does not allow duplicate declarations in the same block scope, which may result in an error

var name; var name; let age; let age; // The Syntax Error identifier Age has already been declared
  1. Variables declared by lets are not promoted in scope
//var raises console.log(name) //undefined var name = 'bear'; // Let does not raise console.log(age) //ReferenceError age is not defined var age = 21;

4. Variables declared by a let at global scope do not become properties of the window object (var declarations do).

var name = 'bear'; console.log(window.name); // bear let age = 21; console.log(window.age); //undefined

Iterated variables defined by the for loop before the let appears are permeated outside the body of the loop

for (var i = 0; i < 5 ; i++){ } console.log(i); / / 5

If we change this to let, the problem becomes trivial, because the iterated variable belongs only inside the for loop block

for (var i = 0; i < 5 ; i++){ } console.log(i); //ReferenceError I is not defined

The most common problem with var is the declaration and modification of iteration variables

for (var i = 0; i < 5 ; I ++){setTimeout(() => console.log(I), 0)} // You might expect to print 0, 1, 2, 3, 4 // Because the variable holds the value after the loop exits, it prints 55 times

This is not the case when using the let variable

for (let i = 0; i < 5 ; I ++){setTimeout(() => console.log(I), 0)

Const declaration

Const is basically like a let, with the only important difference being that when we declare a variable we must also initialize the value of the variable, and trying to modify a const declared variable results in a runtime error

1. Assign values to constants

const name = 'bear'; name = 'jackson' ; / / typeError error

2. Const is also not allowed to duplicate declarations

const name = 'bear'; const name = 'jackson' ; / / SyntaxError error

3. The scope of a const declaration is also a block

const name = 'bear';
if(true){
    const name = 'jackson';
}
console.log(name) //bear

4. The const declaration limit applies to references to the variable to which it refers

const test = {}; test.name = 'bear' ; // It works here

Four,

1. Not using var ES6 is equivalent to unbundling var functions into const and let functions. With let and const functions, you’ll find that you don’t need to use var anymore. Using a const declaration allows the browser runtime to enforce variable invariance, while also quickly catching the unexpected behavior of an unexpected assignment. In short, const defines some variables that will not change. If they do change, let defines them.