JS Built-ins
ObjectObject.freezebeginner

Object.freeze() β€” prevent modification

πŸ“– 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()

Object.freeze(obj) makes an object immutable: you cannot add, remove, or change its properties. In non-strict mode, modifications silently fail.

Challenge: Implement freezeObj(obj) that freezes obj and returns it.

const o = freezeObj({ a: 1 })
o.a = 99   // silently fails
o.a        // β†’ 1
πŸ’‘ Hints (2)
  • β€’ `Object.freeze` returns the same object passed to it.
  • β€’ In sloppy mode, attempts to modify a frozen object silently do nothing.