Grammar
Parameters
Variable1
The name of the variable to declare.
Value1
The initial value assigned to the variable.
Note
use the LET statement to declare a variable whose scope is limited to the block in which it is declared. You can assign a value to a variable when you declare it, or you can assign a value to a variable in a script later.
Variables that use let declarations cannot be used before the declaration, or they will result in an error.
If you do not initialize your variable in a let statement, the JavaScript value undefined is automatically assigned to it.
Example:
var L = ten;
{Let
L = 2;
At this point, L = 2.
}
At this point, L = ten.
Additional ways to declare a is variable using let.
Let index;
Let name = "Thomas Jefferson";
Let answer =, counter, numpages = ten;
Let myarray = new Array ();
Block-level scopes
for (var i = 0; i < i++) {}
Console.log (i),//10 for
(Let j = 0; J < Ten; j) {}
Console.log (j);//"Refe RENCEERROR:J is not defined
There is no variable elevation
Console.log (a); Output undefined
console.log (b);//Error Referenceerror
Console.log (c);//Error Referenceerror
var a = 2;
Let B = 2;
Notice the difference between undefined and referenceerror.
Temporary Dead Zone (TDZ)
as long as you enter the current block-level scope, the variable you are using already exists, but before declaring it belongs to the dead zone and cannot be manipulated.
Note: typeof is no longer a 100% safe operation
typeof X; Referenceerror
typeof y//undefined let
x;
Do not allow duplicate declarations
Let x = 1;
Let X; "Syntaxerror:identifier ' x ' has already been declared
var y = 2;
var y = 3; y = 3
Block-level scopes
anonymous function notation
(function () {
var tmp = ...;
...
} ());
Block-level scope notation
{let
tmp = ...;
...
}
The strict mode of ES5 stipulates that functions can only be declared within the top-level scope and function, and other cases (such as if code blocks, circular blocks) will be declared with an error.
ES5
' use strict ';
if (true) {
function f () {}//Error
}
ES6 because of the introduction of block-level scopes, this situation can be interpreted as a function declared within a block-level scope, so there is no error, but the curly braces that make up the block are not less
Error
' use strict ';
if (true)
function f () {}
Declared global variable is no longer a window's property
"Use strict";
var a = 1;
Console.log (WINDOW.A)//1 let
b = 1;
Console.log (window.b)//undefined