Declare variable keywords:

var let const

Var features:
(2) variable promotion (value = undefined)Copy the code
Let features:
1. Do not repeat the statementCopy the code

function func(arg) { let arg; } func()

2. There is no variable promotionCopy the code

An error ReferenceError

3. Temporary dead zonesCopy the code

Definitions :1. Only valid within the code block in which the let command resides. 2.

eg: var tmp="123"; if(true){ tmp = 'abc'; // Not available before ReferenceError declaration, which binds the block-level scope let temp; }Copy the code

The "temporary dead zone" also means that Typeof is no longer a 100% safe operation. (1) typeof x; // ReferenceError let x; Typeof undeclared_variable // "undefined" (2) {let a = 10; var b = 1; } console.log(a); //a is not defined console.log(b); //1 (3) for(let i=0; i<10; i++){ //... } console.log(i); //i is not defined (4) var a=[]; for(let i=0; i<10; i++){ a[i] = function(){ console.log(i); } } a[6](); //6 The current I is only valid for this loop. The JavaScript engine internally remembers the value of the previous loopCopy the code

Block-level scope

(1)function f1() { let n = 5; if (true) { let n = 10; } console.log(n); (2)ES6 allows arbitrary nesting of block-level scopes. {{{{ {let insane = 'Hello World'} console.log(insane); / / error}}}}; (3) The inner scope can define variables of the same name for the outer scope. {{{{ let insane = 'Hello World'; {let insane = 'Hello World'} }}}}; (4) With the advent of block-level scopes, it is no longer necessary to execute function expressions anonymously immediately. Function () {var TMP =... ; . } ()); // block level scope {let TMP =... ; . } (5) It is allowed to declare functions in block-level scopes, but should be avoided in view of the large variation in behavior caused by the environment. If necessary, write it as a function expression rather than a function declaration statement. {let a = 'secret'; function f() { return a; }} // Inside the block-level scope, the function expression {let a = 'secret'; let f = function () { return a; }; } (6)ES6 block-level scopes must have braces; if there are no braces, the JavaScript engine assumes that there is no block-level scopeCopy the code
Const features:

1. Constants cannot be reassigned

Const PI = 3.1415; PI = 3; // TypeError: Assignment to constant variable.Copy the code

2. Once a const variable is declared, it must be initialized immediately and cannot be left for later assignment

3. Only valid within the block-level scope of the declaration

4. What const actually guarantees is not that the value of a variable is fixed, but that the data stored at the memory address to which the variable refers is fixed

(1) const foo = {}; // Add an attribute to foo, which succeeds foo.prop = 123; Foo. Prop // 123 // Pointing foo to another object will result in an error foo = {}; // TypeError: "foo" is read-only (2) const a = []; a.push('Hello'); // execute a.length = 0; A = ['Dave']; / / an errorCopy the code