In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "what is the difference between let and const in es6", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the difference between let and const in es6".
Differences: 1, let declaration is a variable, its value and type can be changed, while const declaration is constant, can not be changed, can not be re-assigned; 2, let variable, the declaration can not be initialized, and const constant, once declared, it must be initialized immediately; 3, const always points to a fixed address, and let is not fixed.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Let and const are two new ways to declare variables in es6, although we can still use the widely read var variable, but now we have two better tools to use: let and const.
The difference between let and const in es6
① let declares variables whose values and types can be changed; const declares constants whose values and types cannot be changed.
The ② let variable can be uninitialized after declaration, while the const constant, once declared, must be initialized immediately and cannot be assigned later.
Const I; / / error, once declared, must immediately initialize const j = 5 * * j = 10; / / error, constant cannot be changed
③ const always points to a fixed address, while let is not fixed
Const foo = {}; / / add an attribute to foo. If you can successfully foo.prop = 123 position foo.prop / / 123 / / point the foo to another object, it will report an error foo = {}; / / TypeError: "foo" is read-only
In the above code, the constant foo stores an address that points to an object. What is immutable is this address, that is, you cannot point the foo to another address, but the object itself is mutable, so you can still add new properties to it.
Description:
For variables of compound types, such as arrays and objects, the variable name does not point to the data, but to the address where the data is located. Const only guarantees that the address pointed to by the variable name remains the same, not the data at that address, so you must be very careful when declaring a variable of a compound type as a constant.
Const arr = []; / / error is not the same address as [1Jing 2jue 3]; const arr = []; / / No error is reported, the address pointed to by the variable name arr remains the same, except that the data changes arr [0] = 1 Arr [1] = 2 Arr [2] = 3 position console.log (arr.length); / / output: 3
Similarities between let and const in es6:
① is valid only within the block-level scope in which the declaration is located.
There is no declaration promotion in ②, and there is a temporary dead zone, which can only be used after the declared location.
Console.log (dadi); let dadi = 569
Result: will report an error
③ cannot repeat the declaration.
The above is about the content of this article on "what is the difference between let and const in es6". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow 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.