Course Content
Introduction to JavaScript
There is a unique object in JavaScript that calls const
.
Note
const
- is a constant value that can be used like a variable but cannot be changed.
You need to use the const
keyword to define constant:
Note
Constants should be named in UPPER_SNAKE_CASE style. The snake_case means that words will be tied by the
_
symbol. The UPPER means that words will be written in uppercase.
Look at the example with a changeable variable:
Trying to change the value of the constant should cause the TypeError
:
Also, you cannot define a const without a value: it will cause the SyntaxError
:
Usage
Constants are used like unchangeable variables. You can define constant one time and use it multiple times.
Constants provide data integrity with the ability to refactor quickly.
Note
Refactor is a change code structure. (Change names of variables, values, etc.)
For example, MAX_HEIGHT
for site elements. You can release the site update by changing the maximum height of the elements by modifying only one value in the code. However, during runtime, it is not possible to change the maximum height, which ensures the integrity of data:
Constants can receive every value that variables can receive.
Section 2.
Chapter 4