Object.freeze is shallow. For deep immutability, write a recursive deepFreeze function that freezes the object and every nested object. Or use an immutable data library like Immutable.js or Immer.
Common let and const Mistakes in JavaScript
How do you deep freeze an object in JavaScript?
The answer
More FAQs in Common let and const Mistakes in JavaScript
Assuming it makes objects and arrays immutable. const only prevents reassignment of the variable binding. You can still mutate the object's properties. Use Object.freeze for immutability.
Because const requires a value at declaration (const x = 5). The engine catches the missing initializer at parse time, before any code runs, so it is a SyntaxError not a runtime error.
Because i++ reassigns i, and const does not allow reassignment. It throws TypeError: Assignment to constant variable. Use let for classic for loop counters, const for for...of and for...in.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.