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 let and const keywords of JavaScript

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

Share

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

This article mainly explains "how to use the let and const keywords of JavaScript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use JavaScript's let and const keywords".

ES2015 introduces two important new JavaScript keywords: let and const.

Variables defined by const are similar to let variables, but cannot be reassigned:

Example

Const PI = 3.141592653589793

PI = 3.14; / / error will occur

PI = PI + 10; / / can also make an error

Block scope

Variables declared using const within the block scope are similar to let variables.

In this case, x is declared in the block, unlike x declared outside the block:

Example

Var x = 10

/ / here, x is 10

{

Const x = 6

/ / here, x is 6

}

/ / here, x is 10

You can learn more about block scopes in the previous chapter JavaScript Let.

Assign a value on declaration

The JavaScript const variable must be assigned a value when declared:

Incorrect

Const PI

PI = 3.14159265359

Correct

Const PI = 3.14159265359

It's not a real constant.

The keyword const is misleading.

It does not define a constant value. It defines a constant reference to a value.

Therefore, we cannot change the original value of the constant, but we can change the properties of the constant object.

Original value

If we assign a primitive value to a constant, we cannot change the original value:

Example

Const PI = 3.141592653589793

PI = 3.14; / / error will occur

PI = PI + 10; / / can also make an error

At this point, I believe you have a deeper understanding of "how to use JavaScript's let and const keywords". 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