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

How to use the new keywords let and const in ES6

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

Share

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

This article mainly introduces the ES6 new keywords let and const how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

ES6 adds two important JavaScript keywords: let and const

I. let keyword

Variables declared by let are valid only within the block of code where the let command is located.

1. The difference between basic grammar let axiom 123, let and var

Var is also used to declare variables. What's the difference between let and var? The main differences are as follows:

2.1.The same name cannot be defined repeatedly by let in the same scope, and var can be defined repeatedly.

Look at the following example:

/ / the same name cannot be defined repeatedly by let within the same scope. / / error / / var can repeatedly define the same name, let axiom / 123; / / error / / the same name cannot be defined repeatedly in the same scope. / / the same name cannot be defined repeatedly in the same scope.

Let belongs to block-level scope and is valid only within the current block.

Var belongs to the global scope.

Look at the following example:

ES6 _ window.onload=function () {/ / the same let cannot define the same name repeatedly within the same scope / / let axioms 123 words / let aquifers 456; / / error / / var can repeatedly define var axioms 10; var axioms 20 / / has a strict scope, variables belong to the current scope / / let block-level scope {}, var function scope function testVar () {if (true) {var str='123';}; console.log (str) } / / call function testVar (); / / output 123 / / define function function testLet () {if (true) {let str1='456';} console.log (str1) }; / / call function testLet (); / / error report}

Effect:

An error will be reported when printing str1 because it is beyond the scope of str1.

2.3. There is no variable promotion.

There is a variable promotion in the variable declared by var, that is, the variable can be used before the variable is declared. The value is undefined; and there is no variable promotion in let. The variable must be declared before it can be used. Look at the following example:

ES6 _ window.onload=function () {/ / 1, the same name cannot be defined repeatedly by let within the same scope / / let axioms 123 / var / error / / var can be repeatedly defined / / var axioms 10; / / var axioms 20 / / 2. It has a strict scope. The variable belongs to the current scope / / let block-level scope {}, and the var function scope function testVar () {if (true) {var str='123';} / / console.log (str);} / / call function testVar (); / / output 123 / / define function function testLet () {if (true) {let str1='456' } console.log (str1);}; / / call function / / testLet () / / error report / / 3, there is no variable promotion / / var command will cause 'variable promotion' (can be used before declaration, the value is undefined) / / let there is no variable promotion console.log (a); / / undefined var astat12; console.log (b) / / error: b is not defined let baggage 123;}

Effect:

II. Const

What const and let have in common: both are block-level scopes.

The difference: const declares a read-only constant, and changes are not allowed after the declaration. It means that the declaration must be initialized at the same time. Look at the following example:

Const pi='3.1415926';pi='3.1415927'

View the console results:

The declaration must be initialized at the same time, as shown in the example:

ES6 _ window.onload=function () {/ / 1, the same name cannot be defined repeatedly by let within the same scope / / let axioms 123 / var / error / / var can be repeatedly defined / / var axioms 10; / / var axioms 20 / / 2. It has a strict scope. The variable belongs to the current scope / / let block-level scope {}, and the var function scope function testVar () {if (true) {var str='123';} / / console.log (str);} / / call function testVar (); / / output 123 / / define function function testLet () {if (true) {let str1='456' } console.log (str1);}; / / call function / / testLet () / / error report / / 3, there is no variable promotion / / var command will cause 'variable promotion' (can be used before declaration, the value is undefined) / / let does not exist variable promotion / / console.log (a); / / undefined / / var axiom 12; / / console.log (b) / / error report: b is not defined / / let bounded "123"; / / const read-only constant / / same: both are in block-level scope with let / / const pi='3.1415926'; / / pi='3.1415927' / / No modification of const b is allowed / / error: must initialize when declaring}

Console results:

Note:

How does const make sure that variables are not allowed to change after declaration initialization? In fact, what const actually guarantees is not that the value of the variable remains unchanged, but that the data stored in the memory address that the variable points to is not allowed to be changed. At this point, you may have thought that simple types and compound types hold values differently. Yes, for simple types (numeric number, string string, Boolean boolean), the value is stored at the memory address that the variable points to, so the simple type variable declared by const is equivalent to a constant. As for complex types (object object, array array, function function), the memory address pointed to by the variable actually stores a pointer pointing to the actual data, so const can only guarantee that the pointer is fixed, while the data structure that the pointer points to is out of control, so you should be cautious when using const to declare complex type objects.

ES6 _ window.onload=function () {/ / 1, the same name cannot be defined repeatedly by let within the same scope / / let axioms 123 / var / error / / var can be repeatedly defined / / var axioms 10; / / var axioms 20 / / 2. It has a strict scope. The variable belongs to the current scope / / let block-level scope {}, and the var function scope function testVar () {if (true) {var str='123';} / / console.log (str);} / / call function testVar (); / / output 123 / / define function function testLet () {if (true) {let str1='456' } console.log (str1);}; / / call function / / testLet () / / error report / / 3, there is no variable promotion / / var command will cause 'variable promotion' (can be used before declaration, the value is undefined) / / let does not exist variable promotion / / console.log (a); / / undefined / / var axiom 12; / / console.log (b) / / error report: b is not defined / / let bounded "123"; / / const read-only constant / / same: both are in block-level scope with let / / const pi='3.1415926'; / / pi='3.1415927' / / No modification / / const b is allowed / / error: / / array const arr= []; arr.push ('123'); console.log (arr); / / object const obj= {}; obj.name='abc'; console.log (obj) must be initialized when declaring. }

Console results:

Thank you for reading this article carefully. I hope the article "how to use the new keywords let and const of ES6" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you 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