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 and var in js

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the difference between let and var in js". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. The scope of let declaration is block scope, and the scope of var declaration is function scope.

The JavaScript engine records the scope of the identifiers used for variable declarations and their blocks, so nesting using the same identifiers does not report errors because there are no duplicate declarations in the same block.

If (true) {var name = 'Stan' console.log (name) / / Stan} console.log (name) / / Stan if (true) {let age = 20 / / scope is limited to the current block console.log (age) / / 20} console.log (age) / / ReferenceError

2. Another important difference between let and var is that the variables declared by let are not improved within the scope. The moment of execution before the let declaration is called a temporary dead zone.

Console.log (name) / / undefinedconsole.log (age) / / ReferenceErrorvar name = 'xiaoming'let age = 20 what's the difference between let and var in js? thank you for your reading. if you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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