Skip to main content
Practice

Constant

const is short for "constant."

A constant is something that, once set, cannot be changed.

Therefore, a variable declared with const cannot have its value reassigned once it is set.

Example:

declaring a const constant
const birthYear = 1995;

In the example above, the constant birthYear is assigned the value of 1995.

Since birthYear is declared with const, its value cannot be changed once it is set.


Why use const?

  1. To store values that should not change: For example, the default colors of a website or a user's birth year, which should remain constant once set.

  2. Code stability: Using const ensures that values will not be accidentally changed. This can help prevent errors in your program.

  3. Readability: When someone else (or you in the future) reads your code, they can easily understand that variables declared with const will not change, making the code easier to read and understand.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.