Is let the syntax of es6?

WBOY
Release: 2022-04-01 11:24:25
Original
1813 people have browsed it

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;".

Is let the syntax of es6?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

Is let the syntax of es6?

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
Copy after login

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
Copy after login

Is let the syntax of es6?

[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!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!