🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Constants

AI & DATA SCIENCE // constants

const declares a block-scoped variable whose binding cannot be reassigned after initialization.

Syntax

const PI = 3.14159;
const USER = { name: 'Alice', age: 30 };
// PI = 3; // TypeError: Assignment to constant variable

Deep Dive Course

**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.

editor.html
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
localhost:3000

2Practical Example

Here is a real-world application of Constants showing how it is used in production JavaScript code.

editor.html
// 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 1
localhost:3000

3Best 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.

editor.html
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
localhost:3000

Examples

Example 01Basic Usage
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
Example 02Advanced Example
// 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 1

Best Practices

  • Use const by default for all declarations
  • Use Object.freeze() for deep immutability
  • Name constants in SCREAMING_SNAKE_CASE for module-level values

Interview Question

Why can you mutate a const object but not reassign it?

Hint: Think about what the binding actually holds.

const binds a name to a memory address (reference). The address cannot change, but the data at that address can. For primitives (numbers, strings) reassignment would change the value, so it's blocked. For objects, the reference stays the same while the properties change.

Exercises

MediumPractice using Constants in a real scenario.
View Solution
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

Frequently Asked Questions

Why can you mutate a const object but not reassign it?

const binds a name to a memory address (reference). The address cannot change, but the data at that address can. For primitives (numbers, strings) reassignment would change the value, so it's blocked. For objects, the reference stays the same while the properties change.

Related Functions

VariablesData-TypesHoisting