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

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the difference between let const and var". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what is the difference between let const and var"!

Some of the new features in es6 are useful, but sometimes looking at the documentation can be confusing. What is the difference between let, const and var? Let's combine some small examples to show you.

First of all, let's understand the difference between let and var. There are three main points:

The first point is that var supports pre-parsing in javascript, while let does not support pre-parsing. The code is shown in the figure:

The implementation results are shown in the figure:

The result is not wrong, the following var replaced by let, the code is as follows:

Implementation results:

Uncaught ReferenceError: a is not defined, translated into Chinese roughly means a is not defined. This is different from undefined, which is unassigned but defined.

The second point: var can define the same variable repeatedly, but let can not, look at the code:

The implementation results are shown in the figure:

There is no error, replace var with let:

The implementation results are:

Uncaught SyntaxError: Identifier 'a' has already been declared. Translated into Chinese, it means a has been defined.

Third point: let can form block-level scope, javascript before es6 had only function scope, no block-level scope. So how did we achieve block-level scoping before es6? Some friends have guessed it, and immediately execute the function expression, referred to as IIFF. Look at the code:

The implementation results are:

You can see that we implement a local scope or block-level scope with an immediate execution function expression, but with let we don't need to write such code. The code changes as follows.

Implementation results:

You can see that the effect of both is the same.

Here is a classic interview question:

State the results of the above code and explain how it works. Change it to output 0, 1, 2, 3, 4 in sequence

First of all, the above code execution result is: every 200ms print 5 in turn, a total of 5 times.

Why did it turn out like this? This involves two knowledge points in javascript, scope and timer setTimeout callback function asynchronous execution. This code declares a global variable i with var. After the loop is completed, i becomes 5. At this time, the javascript main thread is idle, and the functions in the asynchronous callback queue are sequentially put into the main thread for execution by eventloop. Because i has become 5 at this time, 5 is printed.

The above is the first two points of this topic analysis. And third, how do you modify the code?

Understand the principle is good to transform, since the timer callback function in each i is from the global scope of the value, can you put it in the loop when the local scope, of course, you can see the code:

The implementation results are:

So what does this have to do with let? Recall the third difference between let and var, let can generate local scope, and the code is modified again:

The results of the implementation are:

The above is the difference between let and var. If you have any supplementary comments, welcome to leave a message.

The following is the difference between const and var. The above three points apply to const completely, but const is different from let or var in two points.

First of all, const is used to define constants, and after the constant definition, it is not allowed to change. Look at the code:

Implementation results:

Uncaught TypeError: Assignment to constant variable.

Here is a small pit, see the following code:

Check the results and see if they match your expectations.

No mistakes? Why not? Because the value of a hasn't changed, it still points to the object it was assigned to, and it's reassigned, and if you reassign a, you'll get an error. You can try it. Const constants are allowed to change the structure or properties of reference-type data as long as they refer to it. What are the types of references? Arrays and objects.

The second point is that const defines constants that must be assigned values. If you don't assign, it doesn't mean anything, so you report an error and look at the code:

The implementation results are:

Uncaught SyntaxError: Missing Initializer in Const Declaration It means lack of initial recognition.

At this point, I believe that everyone has a deeper understanding of "what is the difference between let const and var", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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

Internet Technology

Wechat

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

12
Report