Engineering ThoughtFebruary 2026

#CodeUtilize: Object.freeze vs Const

Understanding Immutability in JavaScript by comparing reference safety with content safety.

Object freeze vs Const concept art

// Object freeze vs Const concept art

In JavaScript, we often hear that we should use const for everything. But there's a common trap that even experienced developers fall into: const does not make your data immutable.
It only protects the reference, not the content. Let's explore how to truly secure your data using Object.freeze.

###πŸ›‘οΈ The 'Guard' vs. The 'Vault'

To understand the difference, imagine a Security Guard and a Bank Vault.
  • `const` is the Security Guard. He makes sure the box (variable) isn't replaced by a different box. But he doesn't care if you reach inside and change what's in it!
  • `Object.freeze` is the Bank Vault. Once the data is inside, it's locked. Nobody can reach in and change anything.

###πŸš€ The 'Const' Trap

When you define an object with const, you can still change its properties:
playground.js
const user = { name: "Aldo" };

// This is ALLOWED, even though it's a const!
user.name = "Ignata"; 

console.log(user.name); // Output: "Ignata"
The "Security Guard" (const) only stops you if you try to do this: user = { name: "Someone Else" }; // ERROR: Assignment to constant variable.

###🧊 Freezing for True Security

If you want to ensure a configuration or a set of constants never changes throughout your app's lifecycle, you need to freeze it:
playground.js
const CONFIG = Object.freeze({
  api: "https://api.ignata.dev",
  version: "2.0.0"
});

// This will fail (silently or with error in strict mode)
CONFIG.version = "3.0.0";

console.log(CONFIG.version); // Output: "2.0.0"

###βš–οΈ Comparison at a Glance

Feature`const` πŸ›‘οΈ`Object.freeze` 🧊
Protects ReferenceYesNo (unless used with const)
Protects ContentNoYes
Use CaseDaily variable declarationsCritical Configs & App State
Vibe"Don't swap the box""Don't touch the inside"

###🌈 Conclusion

Immutability leads to fewer bugs and more predictable code. While const is great for general architectural reference safety, Object.freeze is your go-to tool for true data integrity.
Combine themβ€”const MY_OBJECT = Object.freeze({...})β€”for the ultimate multi-layer protection.

If you found this deep dive helpful, check out my GitHub for more JavaScript performance tips and experiments. Happy coding!