Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between let and var in js

Shulou Source: shulou.com Published: 2022-06-03 00:22:28 10月05日 Update

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!

Tags: Function content variable more identity identifier knowledge scope practical same important learned next function region dilemma reality engine situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi Microsoft MySQL Linux