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 difference between let,const and var in JavaScript ES6 syntax

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

Share

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

What is the difference between let,const and var in JavaScript ES6 grammar? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

First, the way variables are declared let / constlet / const have something in common

1. It's all block-level scope.

two。 Variable names are not allowed to be repeated in the same scope

3. The global variable they declared is not hung on the window object

4. There is no pre-compilation.

Differences between let and const

The value of the variable declared by 1.let can be changed

The value of the variable declared by 2.const cannot be changed and must be assigned immediately after declaration (for example: const a = 3.14;)

When 3.const stores the reference data type, the content can be changed (the address cannot be changed)

Give priority to using const, use let if the variable changes, and finally use var

The difference between var and let in for cycle

Var: the variable I is declared by the var command and is valid globally, so there is only one variable I globally. With each loop, the value of the variable I changes, and the I in the function console.log (I) assigned to the timer inside the loop points to the global I. The timer is asynchronous and is executed after the end of the for loop.

For (var I = 1; I < 10; iTunes +) {setTimeout (function () {console.log (I); / / output to 9 10})}

Let: the variable I is declared by let, and the current I is only valid for this cycle, so the I of each loop is actually a new variable, so the final output is 1 2 3 4 5 6 7 8 9 10. Because the value of the previous cycle is remembered inside the JavaScript engine, when initializing the variable I for this round, it is calculated on the basis of the previous cycle.

For (let I = 0; I < 10; iTunes +) {setTimeout (function () {console.log (I); / / 012 3 4 5 6 7 8 9})} after reading the above, have you mastered the difference between let,const and var in JavaScript ES6 grammar? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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