본문으로 건너뛰기

Constants

Constants

const is short for "constant".

A constant means once the value is set, it cannot be changed.

Therefore, a variable declared with const cannot change its value once it has been assigned.

Example:

const birthYear = 1995;

In the example above, the constant birthYear is given the numeric value 1995.

Since birthYear is a variable declared with const, once a value is assigned to it, it cannot be changed again.


Why use const?

  1. To store values that should not change: For example, use const to safely store values that once set, should not be changed, such as a website's base color or a user's date of birth.

  2. Code stability: Using const prevents the value from being changed accidentally, which can reduce programming errors.

  3. Code readability: When you or someone else revisits the code, variables declared with const are understood not to change, making the code easier to understand.