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 is the reason for using let instead of var in es6

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "what is the reason for using let instead of var in es6", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the reason for using let instead of var in es6" can help you solve your doubts, following the editor's train of thought to slowly deepen, together to learn new knowledge.

Because var has no scope in if and for loops, we can only use the scope of function to solve the problem of referencing variables outside the scope; while let has block-level scope in if and for loops, which is a function that var does not have, so es6 uses the let keyword instead of var.

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

Why use let instead of var in es6

Because in the previous JavaScript (before ES5), var had no scope in loops like if and for, and only the scope of function was used to solve the problem of referencing variables outside the scope.

Let is added to ES6, which has block-level scope in if and for, let has block-level scope, and a {} is a scope, that is, let declares block-level variables, that is, local variables.

Const is also scoped at the block level, using const when our modified identifiers no longer want to be changed

It is recommended that you give priority to using const in ES6 development, and use let only if you want to change an identifier.

Be careful

1.const cannot be modified after assignment

two。 Identifiers defined with const must be assigned a value

3. The object to which the constant is defined cannot be modified, but the properties of the object can be modified.

According to general logic, variables should not be used until after the declaration statement, but in the above code, the value of foo is output before declaration, and the value is undefined, which means that the variable already exists before it is declared. Why is there such a problem with var?

This is what people often call variable promotion, that is, variables can be used before declaration, with a value of undefined.

The variable bar declared by our console with let is wrong, which means that the variable bar does not exist before the declaration, so there is no variable promotion. In order to correct this phenomenon and give people a better understanding of the code, the let command changes the syntax behavior, and the variables it uses must be used after the declaration, otherwise an error will be reported.

Read here, this "es6 does not use var but what is the reason for using let" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to pay attention to the industry information channel.

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