let is the syntax of es6. let is a new keyword in es6, which is used to declare variables. The declared variables are only valid within the code block where the let command is located. The let command changes the grammatical behavior. The variables it declares must be used after the declaration, otherwise an error will be reported. , the syntax is "let name=value;".
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
ES6 has added the let command to declare variables. Its usage is similar to var, but the declared variable is only valid within the code block where the let command is located.
The variables declared by let are only valid within the code block where the let command is located.
1. There is no variable promotion:
let command changes the grammatical behavior. The variables it declares must be used after declaration, otherwise an error will be reported.
2. Temporary dead zone:
ES6 clearly stipulates that if there are let and const commands in a block, the variables declared by these commands in this block will be A closed scope is formed from the beginning. Any use of these variables before declaration will result in an error.
In short, within the code block, the variable is not available until it is declared using the let command. Grammatically, this is called the "temporary dead zone" (TDZ).
3. Repeated declarations are not allowed:
let does not allow repeated declaration of the same variable in the same scope. Therefore, parameters cannot be redeclared inside the function.
Examples are as follows:
Basic usage:
{ let a = 0; a // 0 } a // 报错 ReferenceError: a is not defined
Valid within the code block
let is valid within the code block, and var is valid in the global scope :
{ let a = 0; var b = 1; } a // ReferenceError: a is not defined b // 1
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of Is let the syntax of es6?. For more information, please follow other related articles on the PHP Chinese website!