Constants & Magic Values
Replace magic numbers with named constants. Keep status strings, limits and keys in const objects and union types, with TypeScript examples.
On this page
1. What is this practice?
A magic value is a literal that appears in code out of nowhere and does not explain itself: if (attempts > 5), status === "pnd", setTimeout(fn, 86400000). The reader wonders: why 5? What is "pnd"? How long is that number?
| Kind | Example | Better |
|---|---|---|
| Limit | 5 | MAX_LOGIN_ATTEMPTS |
| Duration | 86400000 | ONE_DAY_MS |
| Status string | "pending" in every file | ORDER_STATUS.pending or a union type |
| Storage key | localStorage.getItem("tkn") | STORAGE_KEYS.token |
| Route | "/dashboard/settings" | ROUTES.settings |
2. Why does it matter?
Magic values have two costs: the meaning is hidden, and they are hard to change. If the limit must go from 5 to 10, you search for every 5 and wonder which one belongs to login and which to something else.
| Magic values | Named constants |
|---|---|
| A typo like pendng instead of pending gives no error, the bug hides | TypeScript reports the typo right away |
| What 5 means is a guess | MAX_LOGIN_ATTEMPTS explains itself |
| Changing a limit means searching the whole repo | Change one constant |
| Different values in two files, the UI and the API disagree | One source, one answer |
| A new status is added, some places do not handle it | A Record type shows where it is missing |
3. When to follow it
| Level | How important | Why |
|---|---|---|
| Learning project | Important | An easy habit that saves you from typo bugs |
| Production code | Statuses, limits and keys are always named | Repeated literals are the most common drift bug |
| Senior level | Design where constants live | A module's constants next to its types, shared ones in one place |
4. Bad vs good code
async function login(email: string, password: string) {
const user = await users.byEmail(email);
if (user.failedAttempts >= 5) {
throw new Error("locked");
}
if (user.status === "suspnded") {
throw new Error("suspended");
}
localStorage.setItem("tkn", await auth.token(user, password));
setTimeout(logout, 1800000);
}export const MAX_LOGIN_ATTEMPTS = 5;
export const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
export const USER_STATUS = {
active: "active",
suspended: "suspended",
} as const;
export type UserStatus = (typeof USER_STATUS)[keyof typeof USER_STATUS];
export const STORAGE_KEYS = {
token: "auth_token",
} as const;async function login(email: string, password: string) {
const user = await users.byEmail(email);
if (user.failedAttempts >= MAX_LOGIN_ATTEMPTS) {
throw new LockedError(user.id);
}
if (user.status === USER_STATUS.suspended) {
throw new SuspendedError(user.id);
}
localStorage.setItem(STORAGE_KEYS.token, await auth.token(user, password));
setTimeout(logout, SESSION_TIMEOUT_MS);
}| What is the difference? | Bad | Good |
|---|---|---|
5 | What is it? | MAX_LOGIN_ATTEMPTS |
"suspnded" | A typo that never matches, so a suspended user can log in | USER_STATUS.suspended, a typo is a compile error |
1800000 | Count the zeros | 30 * 60 * 1000, reads as 30 minutes |
"tkn" | Can be spelled differently in every file | One key, one place |
The typo in the first version is a security bug: a suspended user can log in, and no error appears.
A typed accessor, not a magic key
const theme = store.get("thme");const theme = settings.theme();When the key sits behind a typed function, writing the wrong key is simply impossible.
5. How to apply it
6. Exceptions and common mistakes
Not every literal is magic. 0, 1, an empty string, index + 1, or array.length === 0 all have an obvious meaning. Example values in test files ("test@example.com") are fine too.
It is also good to keep text shown to users in one place (needed for translation), but making a constant for every button label is not necessary in a small project.
Mistake: Naming a constant after its value
Why? FIVE = 5 or SECONDS_60 explains nothing, and if the value changes tomorrow the name will lie.
Better: Name it after the purpose, like MAX_LOGIN_ATTEMPTS, not the value.
Mistake: One constants.ts holding the constants of the whole app
Why? Every module's values end up in one file, the file gets huge and everyone edits it.
Better: Keep each constant next to the module that uses it, and only truly shared ones in a shared file.
Mistake: Mixing TypeScript enums and string unions
Why? With an enum in one place and a string in another, comparisons and imports get confusing.
Better: Pick one pattern for the repo, like an as const object plus a derived union type.
Mistake: Keeping a status as an untyped string
Why? status: string accepts any typo.
Better: Give status a union type so only valid values are allowed.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Variables, Self-Documenting Code |
| Alongside | Naming Things, DRY Without Tight Coupling |
| After | Strict Typing, Types Beside Logic, Design Tokens & UI Consistency |
Final quick revision
| Check | Question |
|---|---|
| Meaning | Is the meaning of every number and string clear? |
| Name | Does the constant name describe the purpose, not the value? |
| Set | Are statuses an as const object or a union type? |
| One place | Is a repeated value defined in only one place? |
| Keys | Are storage and state keys behind a typed accessor? |