var declares a variable whose scope is limited to the context of where it was declared
var x = 0; // x is a global variable and is assigned the value 0. console.log( typeof z); // undefined because z doesn't exist yet. function a() { // When a is called, var y = 2; // y is declared as a variable in the scope of function a, and then assigned to 2. console.log(x, y); // 0 2 function b() { // When b is called, x = 3; // The global variable x is assigned to 3, no global variable is generated. y = 4; // The y variable of the existing external function is assigned a value of 4, and a new global variable is not generated. z = 5; // Create a new global variable z and assign 5 to z. } // (Throws ReferenceError in strict mode) b(); // The global variable z is created when b is called. console.log(x, y, z); // 3 4 5 } a(); // B is also called when a is called. console.log(x, z); // 3 5 console.log( typeof y); // undefined, because y is a local variable of a function.
A variable declared by let is only available in the block or sub-block in which it is declared, and the scope of var is the entire enclosing function
function varTest() { var x = 1 ; if ( true ) { var x = 2; // same variable! console.log(x); // 2 } console.log(x); // 2 } function letTest () { let x = 1; if (true) { let x = 2; // different variable console.log(x); // 2 } console.log(x); // 1 }
In ECMAScript 2015, let
bindings are not subject to variable hoisting , which means that let
declarations are not hoisted to the top of the current execution context.
Before the variable in the block is initialized, referencing it will result in ReferenceError
(while declaring a variable with var does the opposite, the value of the variable is undefined )
When used in a block, let
limits the scope of the variable to that block. Note that var
the scope is the area within the function in which it is declared
var a = 1 ; var b = 2 ; if (a === 1) { var a = 11; // the scope is global let b = 22; // the scope is inside the if-block console.log(a); // 11 console.log(b); // 22 } console.log(a); // 11 console.log(b); // 2
A const constant must be declared with its value specified.
const
The declaration creates a read-only reference to a value. But that doesn't mean the value it holds is immutable (like the reference content is an object), it's just that the variable identifier cannot be reassigned
A constant cannot have the same name as another variable or function in its scope
// Note: Constants can be declared in uppercase and lowercase, but usually all uppercase letters are used. // Define constant MY_FAV and assign 7 const MY_FAV = 7 ; // Error MY_FAV = 20 ; // 输出 7 console.log("my favorite number is: " + MY_FAV); // Attempting to redeclare an error const MY_FAV = 20 ; // MY_FAV is reserved for the above constant, this operation will fail var MY_FAV = 20 ; // will also report an error let MY_FAV = 20 ; // Note the nature of the block scope is important if (MY_FAV === 7 ) { // No problem, and a block scoped variable MY_FAV is created // (works equally well with let to declare a block scoped non const variable) let MY_FAV = 20 ; // MY_FAV 现在为 20 console.log('my favorite number is ' + MY_FAV); // this is raised to the global context and raises the error var MY_FAV = 20 ; } // MY_FAV 依旧为7 console.log("my favorite number is " + MY_FAV); // Constant requires an initial value const FOO; // SyntaxError: missing = in const declaration // Constants can be defined as objects const MY_OBJECT = {"key": "value" }; // Rewriting the object will fail as above MY_OBJECT = {"OTHER_KEY": "value" }; // Object properties are not within the scope of protection, the following statement will successfully execute MY_OBJECT.key = "otherValue" ; // It can also be used to define an array const MY_ARRAY = []; // It's possible to push items into the array // It's possible to fill the array with data MY_ARRAY.push('A'); // ["A"] // But , assigning a new array to the variable throws an error MY_ARRAY = ['B']
The above content is excerpted from the MDN Web document to be continued