Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between let and var in es6

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the difference between let and var in es6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Differences: 1, let definition of variables, scope is in the definition of its block-level code and including sub-blocks, while var definition of variables, scope is including its functional scope or global scope; 2, let definition of variables, in the same scope can not be repeatedly declared, while var definition of variables in the same scope can be repeatedly declared.

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

The difference between let and var in es6

Let is a new keyword introduced in ES6 to improve the problems brought by var.

Compared with var, let differs in the following aspects:

Scope

The scope of a variable defined by let is in the block-level code that defines it and the subblocks included in it, and variables cannot be added in the global scope.

The scope of a variable defined by var is the functional scope or global scope that includes it.

Repeat the statement

Variables defined by let cannot be repeatedly declared within the same scope.

Variables defined by var repeat the declaration within the same scope, ignoring the following declaration when generating the execution context.

Problems such as promotion caused by temporary dead zone

We know that before the code is executed, all variables declared by var in the domain are scanned, initialized to undefined, and then executed, which is the so-called "promotion" phenomenon.

However, for variables declared by let, it is different. Scanning before code execution also "promotes" the let variable, but does not set it to undefined. Although the variable defined by let has been promoted, it is not initialized before the code that initializes it is executed, and will be set to a ReferenceError error if accessed at this time. From the start of the code block to the time the let variable is initialized, the let variable has been declared but is not accessible. During this period, it was called a temporary dead zone.

This is the end of the content of "what is the difference between let and var in es6". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report