EMZETT.
Login

Constants

In short: A constant is a named value that must not change after being set, for example PI or MAX_TRIES.

In more detail: Constants replace “magic numbers” in code. Instead of writing 86400 everywhere you use SECONDS_PER_DAY. If the value changes you adjust it in one place.

In detail

  • Java: static final int MAX = 3; (final = cannot be reassigned).
  • JavaScript: const x = 5; (the variable cannot be reassigned, an object inside stays mutable).
  • C/C++: const int x = 5; or #define MAX 3 or constexpr.
  • Python: no true constants, convention: UPPERCASE.
  • Go/Rust: const and let (immutable by default).
  • Naming rule: usually UPPER_WITH_UNDERSCORE.
  • Enums: for groups of related constants (weekdays).

See also: Code comments, Immutability