How to use the let and const keywords of JavaScript
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!