In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use ES6's let and const commands. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1. Let command
① has no block-level scope in js, the variable scope declared by var is the whole function body, and let can play this role.
{let a = 1; var b = 2;} console.log (b); / / 2console.log (a); / / an is not defind
② and let can play this role. In js, the declaration of variables and functions will be promoted to the top of the current scope. This will lead to problems.
Var a = []; / / the function and variable I are declared first, while the global variable I is assigned to 10for (var I = 0; I < 10; iSuppli +) {a [I] = function () {console.log (I);} console.log (I); / / 10a [6] (); / / 10
The use of let solves this problem.
For (let I = 0; I < 10; iTunes +) {a [I] = function () {console.log (I);};} a [6] (); / / 6
Unlike var, ③ let does not have the phenomenon of variable promotion.
Console.log (a); / / an is not definedlet a = 1
④ let does not allow repeated declaration of the same variable within the same block-level scope
/ / error {let a = 10; var a = 1;} / / error {let a = 10; let a = 1;}
2. Const command
① const is also used to declare variables, but to declare constants. Once declared, the value of the constant cannot be changed.
② is the same as let and cannot repeatedly declare the same variable within the same block-level scope.
The scope of ③ const is the same as that of the let command: it is valid only within the block-level scope in which the declaration is located.
Const PI = 3.1415 console.log (PI); / / 3.1415//PI = 3; / / Assignment to constant variable. (constant cannot be assigned) / / const PI = 3.1 PI' has already been declared / Identifier 'PI' has already been declared is all the contents of the article "how to use ES6's let and const commands". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.