PROGRAMMING-CONCEPTS

Constant: Definition, Purpose, and Examples

A constant is a value in a program that cannot change once it has been defined. It represents information that remains fixed throughout a program’s execution — such as mathematical values, configuration settings, or predefined messages. Constants provide reliability and make code easier to understand by signaling that certain data should never be altered.

Across programming languages, constants are declared using slightly different syntax, but the intent is universal: they protect values from accidental modification and improve code safety, readability, and maintenance.


Understanding Constants

In any program, data can either change (variables) or stay fixed (constants). When you mark a value as constant, you’re telling both the interpreter and other developers: this value has meaning and should remain unchanged.

For example, in Python, constants are defined by convention using uppercase letters, since the language itself doesn’t enforce immutability:

PI = 3.14159
MAX_USERS = 100

Although you can technically reassign PI or MAX_USERS, doing so goes against convention. In large codebases, this naming style helps developers quickly recognize values meant to stay static.

In JavaScript and TypeScript, the const keyword makes the restriction explicit:

const MAX_USERS = 100;
const API_URL = "https://api.example.com";

Once assigned, a constant declared with const cannot be reassigned to a new value.

In Swift, constants are defined using let:

let maxUsers = 100
let pi = 3.14159

Swift enforces immutability at the language level — if you attempt to reassign a constant, the compiler will throw an error.


Why Constants Matter

Constants improve code quality in three ways:

  1. Clarity — When you name a constant like MAX_RETRIES or BASE_URL, it communicates intent. Anyone reading the code knows what it represents and that it should not change.
  2. Maintainability — Instead of scattering literal values throughout your program, you define them once. If something needs updating, you modify it in one place.
  3. Safety — Constants prevent accidental reassignment, reducing bugs caused by unintended value changes.

Constants are especially important in projects that depend on stable configurations, such as network endpoints, security keys, or physical constants used in calculations.


Constants in Different Languages

Each language treats constants slightly differently, balancing flexibility and strictness.

  • Python: Constants are conventions, not enforced rules. Developers use uppercase naming (MAX_SPEED) to mark values as unchangeable.
  • JavaScript/TypeScript: The const keyword enforces immutability at the variable level, though the contents of objects or arrays declared as constants can still change.
  • Swift: The let keyword creates true constants, enforced at compile time.
  • C-like languages (e.g., C, Java): Use keywords like const or final to achieve similar effects, often at compile time rather than runtime.

This range of approaches shows that “constant” means the name-to-value relationship is fixed — even if the underlying data might still be mutable (as in constant arrays).


Mutability vs. Reassignment

One of the most misunderstood aspects of constants is that immutability and reassignment are not the same thing.

For example, in JavaScript:

const user = { name: "Luna" };
user.name = "Milo"; // Allowed — the object itself is mutable
// user = { name: "Nova" }; // Not allowed — reassignment blocked

The constant prevents reassignment to a new object, but the contents of the object can still change.

Swift enforces immutability more strictly:

let user = "Luna"
// user = "Milo" // Error: cannot assign to value

When you declare constants, always consider whether you’re protecting the reference (the link to a value) or the data itself.


When to Use Constants

Constants are useful anytime you need a stable value that defines the rules, structure, or environment of your program. Common examples include:

  • Mathematical constants: PI, E, GRAVITY
  • Configuration: API_URL, MAX_CONNECTIONS, DEFAULT_LANGUAGE
  • UI settings: FONT_SIZE, THEME_COLOR
  • Status codes: HTTP_OK = 200, ERROR_NOT_FOUND = 404

Using constants for these purposes makes your code easier to maintain and less error-prone. Instead of updating hardcoded numbers or strings in multiple places, you change the constant definition once and the entire program adapts.


Real-World Example: Configuration Settings

In larger applications, constants often live in dedicated configuration files or modules. This keeps environment-specific data separated from core logic.

For instance, in TypeScript:

export const CONFIG = {
  API_URL: "https://api.example.com",
  MAX_RETRIES: 5,
  TIMEOUT_MS: 3000
};

By centralizing configuration constants, developers can safely adjust settings without touching business logic. The same idea applies to Python with modules like settings.py, or Swift using configuration structs or enums.


Constants in Web Development

Even outside of traditional code, constants play an important role. In CSS, for instance, variables declared with the -- syntax can act as design constants that define color palettes, font sizes, or spacing values.

:root {
  --primary-color: #007bff;
  --font-size-large: 1.5rem;
}

Though technically mutable through runtime changes, these values are typically treated as constants to ensure a consistent design system. This shows that the principle of constants — fixed reference values that unify meaning — applies beyond traditional programming languages.


Constants in Functional Programming

In functional programming paradigms, immutability is a core philosophy. Constants align naturally with this approach because they prevent unintended side effects. Instead of mutating data, functions return new values derived from existing constants.

For example:

const BASE_PRICE = 100;
const TAX_RATE = 0.2;

function calculateTotal(quantity) {
  return quantity * BASE_PRICE * (1 + TAX_RATE);
}

This function is predictable because the constants it relies on never change. Predictability and reproducibility are key advantages of immutability.


Common Mistakes with Constants

The most common mistake is assuming a constant guarantees deep immutability. In languages like JavaScript or Python, only the variable binding is fixed — the internal data might still mutate.

Another issue arises when developers overuse constants. Declaring everything as a constant can make code harder to modify later. Use them strategically: protect values that define rules, not data that’s meant to evolve.

Finally, avoid naming constants inconsistently. Following a clear, recognizable naming convention — such as ALL_CAPS_WITH_UNDERSCORES for global constants — helps keep your code readable and maintainable.


Summary

A constant is a fixed value that gives structure and stability to a program. It signals intent, prevents errors, and improves clarity by marking data as unchangeable.

While implementation varies across languages — from Python’s convention-based constants to Swift’s compile-time enforcement — the idea remains the same: constants represent values that define how your program behaves, not data that fluctuates with execution.

By distinguishing between what can change and what must remain the same, constants make your code not just cleaner, but more reliable — a cornerstone of thoughtful software design.

Learn to Code for Free
Start learning now
button icon
To advance beyond this tutorial and learn to code by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

Reach your coding goals faster