Attempt to modifying 'freeze' object will throw an error?

Based on this learning material:
Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.

The text says there will be no error shown but I’m still getting error while running the code below.


let obj = {
name:"FreeCodeCamp",
review:"Awesome"
};
Object.freeze(obj);
obj.review = "bad";
obj.newProp = "Test";
console.log(obj); 

Here are the error I got:

TypeError: Cannot assign to read only property 'review' of object '#<Object>'
TypeError: Cannot add property newProp, object is not extensible

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.78

Challenge: Prevent Object Mutation

Link to the challenge:

I imagine it depends if the code is run on stricti mode or not, strict mode will make a lot of stuff error out instead of failing silently

freecodecamp editor runs in strict mode

See MDN description of Object.freeze method.

Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, either silently or by throwing a TypeError exception (most commonly, but not exclusively, when in strict mode).

The actual link is here if you want to read more about it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.