JS Built-ins
ObjectObject.freezebeginner

Object.freeze() β€” adding a property fails silently

πŸ“– Usage Example
// Make an object immutable
const obj = Object.freeze({ x: 1 })
obj.x = 99   // silently ignored
obj.x        // β†’ 1

Use Object.freeze() to make an object immutable, preventing any additions, deletions, or value changes.

Object.freeze() β€” no new properties

After freezing, you cannot add new properties to an object. In non-strict mode this fails silently.

Challenge: Implement tryAddToFrozen() that creates a frozen object { a: 1 }, tries to add b: 2, and returns the object's key count.

tryAddToFrozen() // β†’ 1 (only 'a', 'b' was not added)
πŸ’‘ Hints (2)
  • β€’ Adding a property to a frozen object silently fails in sloppy mode.
  • β€’ Check the key count with `Object.keys(o).length`.