Home > Web Front-end > Front-end Q&A > What is the difference between let and var in es6

What is the difference between let and var in es6

WBOY
Release: 2022-03-31 15:39:53
Original
2435 people have browsed it

Difference: 1. The scope of the let variable is in the block-level code in which it is defined, while the scope of the var variable is in the function scope that includes it; 2. The let variable cannot be declared repeatedly, and The var variable can be declared repeatedly; 3. Scanning before code execution will initialize the var variable to undefined, while the let variable is not initialized.

What is the difference between let and var in es6

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

What is the difference between let and var in es6

let is a newly introduced keyword in ES6 to improve various problems caused by var.

Compared with var, there are roughly the following differences between let and var:

1. Scope

  • Pass The scope of variables defined by let is in the block-level code where it is defined and the sub-blocks included therein, and variables cannot be added in the global scope.

  • The scope of a variable defined through var is the function scope or global scope including it.

2. Repeated declaration

  • #Variables defined by let cannot be declared repeatedly in the same scope. .

  • If a variable defined by var is declared repeatedly in the same scope, subsequent declarations will be ignored when generating the execution context.

3. Problems such as promotion caused by temporary dead zone

We know that before the code is executed, the var declarations in all domains will be scanned. Variable, initialize it to undefined first, and then execute the code, which is the so-called "boost" phenomenon.

But for variables declared by let, it is different. The scan before the code is executed also "raises" the let variable, but does not set it to undefined. Although the variable defined by let has been promoted, the variable has not been initialized before the code that initializes it is executed. If accessed at this time, a ReferenceError will be set. From the beginning of the code block to execution to the completion of initialization of the let variable, the let variable has been declared but is inaccessible. This period of time is called a temporary dead zone.

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What is the difference between let and var in 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template