Constant
In one line
A constant is a named value that, once set, cannot be changed while the program runs.
In simple words
A constant is like a variable with a lock on it. You give it a name and a value once, and that value stays the same for the entire life of the program. If you try to reassign it, you get an error.
Constants are useful for values that should never change — mathematical constants like π, configuration values like a maximum number of retries, or fixed labels like a company name. Using a constant instead of a magic number makes your code easier to read and safer to maintain.
Many languages use a special keyword to declare constants: const in JavaScript, final in Java, const in C. The naming convention often uses ALL_CAPS (e.g. MAX_RETRIES) so other developers instantly recognize the value is fixed.
Example
const PI = 3.14159;
console.log(PI); // 3.14159
PI = 3; // ❌ TypeError: Assignment to constant.PI is a constant. Once set to 3.14159, any attempt to reassign it throws an error.
Common confusions
Confused with: variable
The difference: A constant's value is fixed after declaration and cannot be reassigned; a variable can be updated as many times as needed.
Related terms
Related Resources
Related Lessons
- Variables
Variables and constants are introduced together
Related Projects
- Unit Converter
Use constants for fixed conversion factors
Learn the fundamentals
Deepen your understanding with structured lessons on this topic.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.