const with Objects and Arrays in JavaScript
const does not freeze objects or arrays. Here is what const actually prevents and how to get true immutability.
const with Objects and Arrays in JavaScript
A common misconception: const makes objects and arrays immutable. It does not. const only prevents reassignment of the variable binding.
What const Prevents
const obj = { a: 1 }; obj = { b: 2 }; // TypeError: Assignment to constant variable `` You cannot reassign `obj` to a new object. The binding is fixed. ### What `const` Does NOT Prevent ```js const obj = { a: 1 }; obj.a = 2; // OK (mutating a property) obj.b = 3; // OK (adding a property) delete obj.a; // OK (removing a property) const arr = [1, 2, 3]; arr.push(4); // OK arr[0] = 99; // OK arr.pop(); // OK `` Mutating the object or array is allowed. `const` is about the binding, not the value. ### `Object.freeze` for Shallow Immutability ```js const obj = Object.freeze({ a: 1 }); obj.a = 2; // silently fails (or throws in strict mode) obj.b = 3; // silently fails console.log(obj); // { a: 1 } `` `Object.freeze` prevents property changes, additions, and deletions. But it is **shallow**: nested objects can still be mutated. ```js const obj = Object.freeze({ nested: { a: 1 } }); obj.nested.a = 2; // OK (freeze is shallow) console.log(obj.nested.a); // 2 `` ### Deep Freeze For deep immutability, recursively freeze: ```js function deepFreeze(obj) { Object.keys(obj).forEach(key => { if (typeof obj[key] === "object" && obj[key] !== null) { deepFreeze(obj[key]); } }); return Object.freeze(obj); } `` ### `const` + `Object.freeze` Together ```js const config = Object.freeze({ apiUrl: "https://api.example.com", timeout: 5000, }); `` This prevents both reassignment (`const`) and mutation (`freeze`). It is a common pattern for configuration objects. ### The Takeaway `const` prevents reassignment of the binding, not mutation of the value. Objects and arrays can still be mutated. Use `Object.freeze` for shallow immutability, or a deep freeze for nested structures. Use `const` + `freeze` for configuration that should never change.
No. const prevents reassignment of the variable, but you can still mutate the object's properties (add, change, delete). Use Object.freeze for immutability.
const prevents reassignment of the variable binding. Object.freeze prevents mutation of the object itself (no property changes, additions, or deletions). They work at different levels: const is about the variable, freeze is about the value.
Shallow. Object.freeze prevents changes to the top-level object's properties, but nested objects can still be mutated. For deep immutability, you need a recursive deepFreeze function.
Yes. const prevents reassigning the array variable, but you can still mutate the array with push, pop, splice, and index assignment. Use Object.freeze (or Object.freeze(arr)) to prevent mutations.
Use const plus Object.freeze together. const prevents reassignment of the variable, and Object.freeze prevents mutation of the object's properties. For nested objects, use a deep freeze function.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

