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 javascript's var and let,const?

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

Share

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

This article mainly talks about "what is the difference between javascript's var and let,const". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between javascript's var and let,const?"

Speaking of several ways to declare variables in JavaScript, that is, var, let, and const, let and const are new commands in es6. So what's the difference between them?

First of all, let's talk about the differences among the three. In the detailed introduction, the differences among var, let and const are mainly analyzed from the following points:

As a global variable.

Variable lifting

Temporary dead zone

Block level scope

Repeat the statement

Modify declared variables

As a global variable

In ES5, the properties of top-level objects are equivalent to global variables, and variables declared with var are both global variables and attributes of top-level variables.

But variables declared with let in ES6 can be accessed globally, but they are not on the properties of the top-level variables, and the constants declared by const are also not on the top-level variables.

Variable lifting

The variable declared by var has a variable promotion, and the variable can be called before declaration, with a value of undefined

There is no variable promotion in let and const, that is, the variables they declare must be used after declaration, otherwise an error will be reported.

Console.log (a) / / undefinedvar a = 1console.log (b) / / Cannot access 'b'before initializationlet b = 2console.log (c) / / Cannot access 'c'before initializationconst c = 3console.log (a) / / undefinedvar a = 1console.log (b) / / Cannot access 'b'before initializationlet b = 2console.log (c) / / Cannot access 'c'before initializationconst c = 3 temporary dead zone

There is no temporary dead zone in var

There is a temporary dead zone between let and const. Only after the line of code that declares the variable is executed can you get and use the variable.

In fact, this is the difference between the last variable promotion and extension. Because there is a variable promotion in the variable declared by var, using the variable value of undefined before the declaration will not report an error, so there is no temporary dead zone. Let and const will report an error before and after the start of the scope and before the declaration of variables or constants. This area is also called a temporary dead zone.

Example is the same as above:

Console.log (a) / / undefinedvar a = 1console.log (b) / / Cannot access 'b'before initializationlet b = 2console.log (c) / / Cannot access 'c'before initializationconst c = 3 block level scope

Var does not have block-level scope

Let and const have block-level scope

{var a = 2} console.log (a) / / 2 {let b = 2} console.log (b) / / Uncaught ReferenceError: b is not defined {const c = 2} console.log (c) / / Uncaught ReferenceError: c is not defined repeat declaration

Var allows repeated declarations in the same scope, and subsequent declarations will override the previous variable declarations.

Let and const are not allowed to repeat declarations in the same scope.

Var a = 10var a = 20 / / 20let b = 10let b = 20 / / Identifier 'b'has already been declaredconst c = 10const c = 20 / / Identifier 'c'has already been declared modifies declared variables (constants and variable declarations)

Var and let declare variables, and the declared variables can be modified

Const declares the gain and loss constant, read-only. Once declared, the value of the constant cannot be changed. However, it should be noted that for reference data types, variables or constants in JavaScript store the storage address of the data, as long as the reference to the constant is not directly modified, it is possible to change the properties of the object it points to.

Var a = 10a = 20console.log (a) / / 20let b = 10b = 20console.log (b) / / 20const c = 10c = 20 / / Uncaught TypeError: Assignment to constant variable so far, I believe you have a deeper understanding of "what is the difference between var and let,const in javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow 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

Development

Wechat

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

12
Report