**const** prevents **rebinding** of the variable name, but it does NOT make objects or arrays immutable. You can still mutate object properties or push to arrays. For true immutability use `Object.freeze()`.
1Understanding Constants
const prevents rebinding of the variable name, but it does NOT make objects or arrays immutable. You can still mutate object properties or push to arrays. For true immutability use Object.freeze().
const is about the binding, not the value. Object properties can still be changed.
const PI = 3.14159;
console.log(PI); // 3.14159
// PI = 3; // TypeError!
const config = { debug: false, version: '1.0' };
config.debug = true; // OK - mutating property
console.log(config.debug); // true2Practical Example
Here is a real-world application of Constants showing how it is used in production JavaScript code.
// Deep immutability with freeze
const frozen = Object.freeze({ x: 1, y: 2 });
frozen.x = 99; // silently fails (or throws in strict mode)
console.log(frozen.x); // still 13Best Practices
Follow these guidelines when working with Constants:
1. Use const by default for all declarations
2. Use Object.freeze() for deep immutability
3. Name constants in SCREAMING_SNAKE_CASE for module-level values
Tip: const is about the binding, not the value. Object properties can still be changed.
const PI = 3.14159;
console.log(PI); // 3.14159
// PI = 3; // TypeError!
const config = { debug: false, version: '1.0' };
config.debug = true; // OK - mutating property
console.log(config.debug); // true