JavaScript: Constants
Sometimes a program contains values that should never change. For example:
- the mathematical constant π (pi);
- the dollar exchange rate on a specific date;
- a fixed service fee.
Such values are called constants, and it is customary to distinguish them from regular variables so that there is no temptation to change them.
Example: the number π
const PI = 3.14;
console.log(PI); // => 3.14Here PI is a constant that stores the value of the number π. The point of a constant is that its value should not change during the program's execution.
const in JavaScript
In many languages, a constant is a separate language entity whose value cannot be changed. In JavaScript, the keyword const is used for this. Unlike a regular variable, a constant declared with const cannot be reassigned:
const maxLoginAttempts = 3;
maxLoginAttempts = 5; // TypeError: Assignment to constant variable.const vs let
let score = 0; // will change
const lives = 3; // does not changeUse const by default. Switch to let only when you know for certain that the value will be reassigned. Constants are easier to reason about: when you see const, the reader immediately knows that the value will not change anywhere in the code. With let, there is no such certainty, and you have to track all the places where the variable could have been modified.
Naming convention
Technically, any variable declared with const is already a constant. But for global constants — those that define the fundamental parameters of the entire program — it is additionally customary to use the UPPER_SNAKE_CASE style (also called SCREAMING_SNAKE_CASE):
- all letters are uppercase;
- words are separated by the underscore character
_.
const MAX_USERS = 100;
const DEFAULT_TIMEOUT = 30;
const DEFAULT_LANGUAGE = 'ru';This is not a language requirement but a signal to the reader: such a value should not change under any circumstances.
Why do we need constants?
Constants make code clearer and safer. They help you see right away which values in the program are considered fixed. This is especially important when working with data such as mathematical and physical constants, default settings, or fixed limits. Using constants reduces the risk of errors: from the const declaration, it is immediately clear that we are dealing with a value that should not be changed. In addition, if the value does need to be changed (for example, in settings), it is enough to change it in one place, and the change is automatically picked up throughout the entire program.
Instructions
Create a constant MAX_LOGIN_ATTEMPTS with the value 3 and print it to the screen.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
// @ts-check
import { expect, test, vi } from 'vitest';
test('constants', async () => {
const consoleLogSpy = vi.spyOn(console, 'log');
await import('./index.js');
const firstArg = consoleLogSpy.mock.calls.join('\n');
expect(firstArg).toBe('3');
});Teacher's solution will be available in:
20:00
