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 similarities and differences of var,let,const?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what are the similarities and differences of var,let,const, I believe that most people still do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

I. the difference between let and var

1. On variable promotion, var energy variable promotion, let can not

/ / console.log (a) as shown below for var; / / output undefined, at this time, the variable is promoted var aqum2; console.log (a); / 2 target / equivalent to the following code var a; / / declared and initialized to undefinedconsole.log (a); / / output undefineda=2; / / assign console.log (a); / 2 console.log / about let as shown below (a); / / error ReferenceErrorlet astat2 / / it is equivalent to declaring a but not initializing it on the first line, and it is not initialized until the assignment time. / / if you directly use let to declare that the variable does not assign a value, undefined will be printed. At this time, let a share console.log (a) will be initialized; / / the value will be undefined.

two。 Temporary dead zone: there is a let command in the block-level scope, and the variables it declares are "bound" to this area and are no longer affected by external influences. In short, a code block has let instructions, even if there are external variables with the same name, the variables of the same name and external variables do not interfere with each other. Var does not, as shown below:

/ / letvar a = 123 true if (true) {let a = "abc"; console.log (a); / / output abc} console.log (a); / / output value is 123, global an and local a do not affect each other / / varvar a = 123 var a = "abc"; console.log (a); / / output abc} console.log (a); / / output value is abc, the global value has been changed

In short, within the code block, the variable is not available until the let command is used to declare it. Grammatically, this is called a "temporary dead zone" (temporal dead zone, referred to as TDZ). Examples are as follows:

Var tmp=1;if (true) {/ / TDZ starts tmp= 'abc'; / / ReferenceError console.log (tmp); / / ReferenceError let tmp; / / TDZ ends console.log (tmp); / / undefined tmp= 123; console.log (tmp); / / 123} console.log (tmp); / /

Within the code block bound by 3.let declaration, the same variable cannot be declared repeatedly. Var can

/ / A cannot repeat function sub () {let a = 10; var a = 1;} / / report an error, Identifier 'a'has already been declaredfunction sub () {let a = 10; let a = 1;} / ditto function sub () {let a = 10; {let a = 1;} / / is not in the same code block at this time, no error can be reported} / / var can be repeatedly declared and will not report error function sub () {var a = 10; var a = 1;}

4. Similar to the code block of the for loop, let is only valid inside the code block, and var is also valid outside the code block.

/ / let is only valid inside the code block for (let I = 0; I < 10; iCool +) {} console.log (I); / / error ReferenceError: i is not defined//var is also valid outside the code block for (let I = 0; I < 10; iLike +) {} console.log (I); / / what makes 101let special within the for loop is that the part that sets the loop variable is a parent scope, while the inside of the loop body is a separate child scope. / only in the parent scope var a = []; for (let I = 0; I < 10; iSum +) {a [I] = function () {console.log (I);};} a [6] (); / / 6Accord / child scope redeclare var a = []; for (let I = 0; I < 10; iSense +) {a [I] = function () {let iaccoun3; / / reassign console.log (I);} } a [6] (); / / 3, get a new value

II. Let and const

1. Similarities:

A, the variable does not improve.

B, temporary dead zone, can only be used after the declared position.

C. non-repeatable declaration.

2. Differences:

Variables declared by let can be changed.

Const declares a read-only constant. Once declared, the value of the constant cannot be changed, and the assignment must be initialized when declared.

Let a; / / undefined

Const b / error / error must be assigned when declaring

Let axi1

Axi2; / / changeable

Const bust 1

Error 2; / / error cannot be changed

/ / some points that I think I should pay attention to.

Let aura null; / / a=null

Undefined; / / a=undefined

Axi2; / / axi2

Const is null; / / null and undefined can also be defined in Const.

Const bounded undefined; / / b=undefined

Error 2; / / error cannot be changed

Essence:

What const actually guarantees is not that the variable cannot be changed, but that the data stored in the memory address where the variable points to will not be changed.

A. five basic data types (Number,String,Boolean,Undefined,Null): the value is stored in the memory address that the variable points to, which is equivalent to the constant. The value cannot be changed.

B, complex data type (Object: array, object): the variable name of this type does not point to the data, but points to the address where the data is located. Const only ensures that the address pointed to by the variable name remains the same, and does not guarantee that the data changed to the address remains the same, so you can modify the attribute value of the address, but cannot change the address direction.

Const a = []; a.push ("Hello"); / / executable, the attribute value of changing the address can be modified. Executable, as above a = ["Tom"]; / / error, cannot change the address to point to const b = {}; b.prop123; / / add an attribute to b, you can successfully b.prop / / 123b = {} / / pointing b to another address will cause an error. If you really want to freeze the object, you should use the Object.freeze method. Const b=Object.freeze ({}); / / in regular mode, the following line does not work. When b.prop is in undefined// strict mode, the bank will report an error b.prop = 123. This is all the content of the article "what are the similarities and differences of var,let,const?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.

Share To

Development

Wechat

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

12
Report