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 var and let in JavaScript

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares with you is about what is the difference between var and let in JavaScript. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Foreword:

Var is a variable declaration keyword that existed when JavaScript first appeared, while let as a variable declaration keyword just appeared in ES6, there is no doubt that there is a big difference between the two. So what are the specific differences?

1. Scope is expressed in different forms.

Var is a function scope, let is a block scope

{var monkey=' fumigong'; let pig=' pork chop cover';} console.log (monkey); / / output undefinedconsole.log (pig); / / error report: pig is not deined

As can be seen from the above code, the variables declared by let are valid only in the code block in which they are located, and are not valid and cannot be accessed outside the code block, while the variables declared by var are valid in the function scope of the code block.

two。 Whether or not the difference between variable promotion

Variables declared by var are promoted, while variables declared by let are not promoted.

Console.log (monkey); / / undefinedvar monkey=' fumigong'; console.log (pig); / / error report: pig is not definedlet pig=' pork chop lid'

The same logic, why the variable declared by var will be undefined when called before it is declared, while the variable declared by let will throw an exception when called before it is declared. This is the difference in variable promotion between the two. Variables declared by var have variable promotion, while variables declared by let do not have variable promotion.

So what is variable promotion? I will not describe it conceptually here. I will only say that my personal understanding is that the above code is actually equivalent to the following:

Var monkey;console.log (monkey); / / undefinedmonkey=' fumigong'; console.log (pig); / / error report: pig is not definedlet pig=' pork chop lid'

See the difference, the variable declared by var will extract the declared variable to the top of the scope to be defined but not assigned, and the assignment operation is still in your code, so when you call the variable declared by var, it is a variable that has been declared but not defined, so the result of the call is undefined, which is called variable promotion. There is no such variable promotion in the variables defined by let.

3. The difference in temporary dead zone

Temporary dead zone: if a variable is let in a scope, if there is a variable with the same name in the external scope, then even if a change is made in the scope, it will not affect the external scope

The specific performance is as follows:

For (var iTuno Bandi)

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