InterviewHubPRO
Section Overview
Q482

Question 482 of 521

Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 521)

93%
JavaScript Internals (521 Qs)•Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 521)

What is the difference between const and Object.freeze

The main difference is that const applies to variables (bindings), while Object.freeze() applies to values (objects).

  1. const: Prevents the reassignment of a variable identifier. It ensures that the variable name always points to the same memory reference. However, if the variable holds an object or array, the contents of that object can still be modified.
  2. Object.freeze(): Prevents the modification of an object's properties. It makes the object immutable (you cannot add, remove, or change properties), but it does not affect the variable assignment itself (unless the variable is also declared with const).

Example:

javascript
// Case 1: Using const (Reassignment prevented, Mutation allowed)
const person = { name: "John" };
person.name = "Doe"; // ✅ Allowed: The object is mutable
console.log(person.name); // "Doe"

// person = { name: "Jane" }; // ❌ Error: Assignment to constant variable

// Case 2: Using Object.freeze (Reassignment allowed, Mutation prevented)
let profile = { name: "John" };
Object.freeze(profile);

profile.name = "Doe"; // ❌ Ignored (or throws TypeError in strict mode)
console.log(profile.name); // "John"

profile = { name: "Jane" }; // ✅ Allowed: 'profile' is declared with 'let'
console.log(profile.name); // "Jane"