const
constcreates a constant binding — once assigned, the variable name can’t be reassigned.
Why Do We Need This?
Section titled “Why Do We Need This?”const protects you from accidentally changing important values. It also signals to other developers: “this value should not change.”
Key Rule
Section titled “Key Rule”const PI = 3.14;PI = 3.14159; // ❌ TypeError: Assignment to constant variableImportant:
constdoes NOT make the value immutable. Only the binding is constant.
const user = { name: "Alice" };user.name = "Bob"; // ✅ This is ALLOWED!user = {}; // ❌ TypeError — can't reassignVisual Explanation
Section titled “Visual Explanation”flowchart TD A["const x = 5"] --> B["x = 10 ❌ Error"] A --> C["x = 'hello' ❌ Error"]
D["const obj = { name: 'Alice' }"] --> E["obj.name = 'Bob' ✅ Allowed"] D --> F["obj = {} ❌ Error"]
style A fill:#10b981,color:#fff style D fill:#f59e0b,color:#fff style B fill:#ef4444,color:#fff style C fill:#ef4444,color:#fff style F fill:#ef4444,color:#fff style E fill:#10b981,color:#fffCode Examples
Section titled “Code Examples”// 1. Must initialize// const x; // ❌ SyntaxError: Missing initializer
// 2. When to use constconst API_URL = "https://api.example.com";const MAX_RETRIES = 3;const USER_ROLES = { admin: "Admin", user: "User" };
// 3. const with objects — can mutate propertiesconst person = { name: "Alice", age: 30 };person.age = 31; // ✅ Allowedperson.city = "NYC"; // ✅ Allowed// person = {}; // ❌ Error
// 4. const with arrays — can mutate contentsconst numbers = [1, 2, 3];numbers.push(4); // ✅ Allowednumbers[0] = 10; // ✅ Allowed// numbers = []; // ❌ Error
// 5. const in loops — works with for...ofconst items = [1, 2, 3];for (const item of items) { console.log(item); // ✅ Allowed — new binding per iteration}🚀 Best Practices
Section titled “🚀 Best Practices”- Default to
const— use it for everything by default - Only change to
letwhen you need reassignment - Use
Object.freeze()for truly immutable objects:
const config = Object.freeze({ apiUrl: "https://api.example.com", timeout: 5000,});config.timeout = 10000; // ❌ Fails silently (or TypeError in strict mode)✅ Summary
Section titled “✅ Summary”| Feature | const |
|---|---|
| Reassign | ❌ Error |
| Mutation | ✅ Allowed |
| Hoisting | ✅ (TDZ) |
| Must initialize | ✅ Yes |
| Modern usage | ✅ Default choice |