When an object is frozen with Object.freeze(), its properties can’t be changed. In non-strict mode the attempt is ignored, while in strict mode it may throw. This keeps objects stable and predictable, a key idea in JavaScript immutability and robust coding practices.

Multiple Choice

What happens if you attempt to modify a frozen object's property?

The correct answer highlights an important characteristic of frozen objects in JavaScript. When an object is frozen using `Object.freeze()`, it prevents any changes to the object's properties, maintaining the integrity and immutability of that object. Therefore, if you attempt to modify a property of a frozen object, the change will not take effect, and the original value will remain unchanged. This behavior is significant because it enforces a level of immutability, making the object stable and predictable throughout the execution of the program. While the modification attempt is silently ignored in non-strict mode, the key takeaway is that the original state of the frozen object is preserved. The other answer choices each present scenarios that misrepresent how frozen objects operate. For instance, saying that the change will succeed silently implies that the modification actually takes effect, which contradicts the mechanics of frozen objects. Similarly, saying that modification is allowed only through reassignment suggests that one can still alter properties when the object is frozen, which is not true. In short, the essence of this concept is centered around immutability and the specific behaviors brought about by freezing an object.

When you’re building modern JavaScript apps—whether you’re scripting a Lightning Web Component or just tinkering with a Node script—immutability isn’t a buzzword. It’s a sturdy compass that helps keep state predictable. One of the simplest, most useful ways to enforce immutability is by freezing an object with Object.freeze(). If you’ve ever wondered what happens when you try to poke at a frozen object, you’re about to get a clean, practical explanation that sticks.

First, the basic idea: what does it mean to freeze an object?

Think of Object.freeze() as a shield. Once an object is frozen, you can’t add new properties, delete existing ones, or modify the values of current properties. The object’s shape and its values stay as they are—permanently, at least for that object’s lifetime. This is especially handy when you want to preserve an important configuration, a constant set of data, or a reference you don’t want accidentally altered as your code runs.

What actually happens when you try to change a property?

Here’s the punchline you’ll likely remember: if you attempt to modify a property of a frozen object, the change will not affect the original value. In other words, the mutation that you tried to apply simply doesn’t stick.

But there are two practical flavors to this behavior, depending on how your code runs:

  • In non-strict mode (the more forgiving, legacy way JavaScript often runs): the attempt to change a frozen property silently fails. No error is thrown, and you won’t see the change take place. The original value remains intact, as if you’d never pressed the edit key.

  • In strict mode (the more disciplined, newer habit in coding): attempting to modify a frozen object will throw a TypeError. You’ll get a clear signal that something immutable is resisting your change, which can be a helpful guardrail during development.

That dual behavior mirrors JavaScript’s broader philosophy: give developers a safe default, but offer a way to catch missteps when you opt into stricter rules. If you’re working with code that might run in diverse environments or when you’re debugging, knowing whether you’re in strict mode can save you some head-scratching.

A quick illustration to keep things concrete

  • Suppose you have an object:

const config = Object.freeze({ apiEndpoint: "https://api.example.com", timeout: 5000 });

  • If you try to change a property:

config.timeout = 10000;

  • In non-strict mode, nothing happens. The object remains with timeout: 5000.

  • In strict mode, this line would throw a TypeError, and your code would stop unless you catch it.

Why this matters in real-world apps

Immutability is more than a neat trick; it’s about predictability and safer state management. In Salesforce development, you often deal with data that flows through components, event handlers, and service calls. If you accidentally mutate an object that’s meant to be a stable reference—say, a configuration object or a cache of constants—you can introduce subtle bugs that are hard to track down. Freezing those objects gives you a reliable baseline.

And there’s more depth to it than “don’t touch this.” Freezing a simple object is straightforward, but real-world data often comes in layers. You might freeze a top-level object and then encounter nested objects inside it. By default, Object.freeze() only freezes the immediate properties; nested objects remain mutable unless you explicitly freeze them too. This is a common pitfall, and it’s worth calling out so you don’t assume a single freeze makes the whole structure immutable.

A practical approach: making nested structures truly immutable

If your goal is deep immutability, you’ll want to freeze recursively. Here’s a small pattern you can use:

function deepFreeze(obj) {

Object.keys(obj).forEach((key) => {

const value = obj[key];

if (value && typeof value === "object") {

deepFreeze(value);

}

});

return Object.freeze(obj);

}

const settings = deepFreeze({

api: {

endpoint: "https://api.example.com",

retry: 3

},

theme: { color: "blue" }

});

With this, you get a rock-solid structure where nested objects are frozen too. It’s a little more code, but the payoff is confidence that nothing, not even a nested layer, will drift.

Immutability in the wild: when to freeze and when not to

Freezing is a great tool, but it isn’t a universal fix. There are times when you actually want to update an object’s properties. If you’re working with state in a UI framework, you might prefer to replace the whole object rather than mutate it in place. This helps libraries and hooks detect changes more reliably and can lead to cleaner render logic.

In many Salesforce scenarios, you’ll see patterns where you’ll create new objects from existing ones, rather than mutating the originals. For example, when you handle data coming from a Lightning Data Service wire adapter or a custom Apex call, you often map or copy data into a new object, then work with that copy. It’s a gentle discipline that reduces side effects and makes your components easier to reason about.

A few caveats worth noting

  • Object.freeze() is shallow by default. If you freeze an object with a nested object inside, you’ll still be able to mutate the nested object unless you freeze that nested object as well. That’s the difference between “frozen at the top level” and “fully immutable.”

  • Functions inside a frozen object aren’t magically protected from being replaced. The properties themselves can’t be reassigned, but you can still replace the function variable if you rebind it somewhere else in your code. In other words, freezing helps protect values, not the entire identity of a binding in every scope.

  • Console tools can help you confirm behavior. If you’re unsure whether a property is really frozen, try assigning a new value in a dev environment with strict mode on and watch for errors. It’s a reliable way to verify your understanding.

Bringing it back to the bigger picture

Immutability is a quiet hero in software development. It won’t grab the spotlight like a fancy framework feature, but it quietly does a lot of heavy lifting. When you freeze an object, you’re making a promise to your future self: “You can rely on this data as-is.” That’s a reassuring posture when your codebase starts to scale and multiple parts of the system touch the same objects.

In the Salesforce ecosystem, where components must work together smoothly and data flows through many layers, this kind of discipline pays off. It helps prevent you from accidentally mutating a shared resource, reduces the risk of race conditions, and makes debugging a little less of a scavenger hunt. And yes, it’s perfectly compatible with modern JavaScript modules, Lightning Web Components, and most tooling you’ll encounter on the platform.

Analogies that stick

Think of an immutable object like a pressed leaf in a catalog. You can flip it, admire the color, or press it between pages, but you don’t tear out the leaf and scribble on it. The leaf remains exactly as it was the moment it was pressed. If you need a new leaf, you press a new one—copy the old shape, change a detail, and you’ve got something fresh without disturbing the original specimen.

A small vocabulary boost you can carry into your next coding session

  • Shallow freeze: freezes only the immediate properties.

  • Deep freeze: freezes the entire nested structure so every layer is immutable.

  • Strict mode: a mode that enforces certain behaviors more rigorously, including throwing errors on attempts to mutate frozen objects.

Let me explain why this matters in everyday coding

When you’re building components that rely on a stable configuration or a shared data snapshot, freezing helps your code behave like a well-tuned instrument. You won’t get surprised by a property morphing under the hood, and you’ll find it easier to reason about how data travels through your app. It keeps your mental model aligned with what the code actually does.

A little practical Q&A you can tuck away

  • Q: If I freeze an object and later try to add a new property, what happens?

A: In non-strict mode, the addition is ignored; in strict mode, you’ll get a TypeError.

  • Q: Does freezing protect methods, too?

A: It prevents changing property values and the structure, but you can still replace the function reference if you rebind it elsewhere. The function itself won’t mutate just by freezing.

  • Q: Should I freeze everything?

A: Not every object needs it. Use your judgment. Freeze where you want to preserve a stable baseline, especially for constants, configuration, and data you know should not drift.

Pulling it all together

In the end, the act of freezing an object is a small, clear statement about intent. It says, “This is fixed. Do not alter.” For developers who value predictable behavior and clean state transitions, that’s a comforting stance to take. It’s a simple tool with a big payoff: fewer surprises, clearer data flow, and a sturdier foundation for building scalable, reliable software—whether you’re crafting a sleek Lightning Web Component or refining a Node-based utility.

If you’re curious to experiment, try freezing a couple of objects in a little sandbox. Start with a config object you use across a module, then push a nested layer and see how far you can go before you trip on an error in strict mode. The firsthand feel of it sticks better than any abstract explanation, and you’ll walk away with a practical intuition you can bring into daily coding life.