Constant Declaration Keyword, const
const is short for Constant, which means that once a value has been set, it cannot be changed.
Therefore, a variable declared using const cannot have its value altered once it is assigned.
const birthYear = 1995;
In the example above, the constant birthYear is assigned the numeric value 1995.
Since birthYear is declared with const, its value cannot be changed once set.
The scope of a const declared variable, like let, is block-level scope.
Thus, a variable declared with const inside curly braces is not accessible outside that code block.
Why Use const as a Constant Variable?
-
To Store Values That Should Not Change: It is used when you want to safely store values that should not change once set, such as a website's default color or a user's birth year.
-
Improving Code Stability: By using
const, you prevent the value from being accidentally modified. This helps to reduce errors in your program, making it more robust.
Lessons in this chapter ยท Getting Started with JavaScript
- 1. What is JavaScript?
- 2. What are Programming Languages?
- 3. How to Instruct a Computer
- 4. Comment
- 5. Multiple-choice quiz
- 6. Data Types
- 7. Variable Declaration Keyword - var
- 8. Scope
- 9. Variable Declaration Keyword - let
- 10. Constant
- 11. Operators
- 12. Variable Assignment
- 13. Identifiers and Literals
- 14. Fill-in-the-blank quiz
A constant declared with const can have its value changed.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help