course content

Course Content

Introduction to JavaScript

constconst

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.

1. Define a variable:
2. Define a constant:
question-icon

Define a variable:

_ _ _
_ _ _
= 50;

Click or drag`n`drop items and fill in the blanks

dots
MAX_WIDTH
dots
let
dots
const
dots
someWidth;
question-icon

Define a constant:

_ _ _
_ _ _
= 50;

Click or drag`n`drop items and fill in the blanks

dots
const
dots
someWidth
dots
let
dots
MAX_WIDTH

Section 2.

Chapter 4