Object.seal()
Object.seal()
Section titled “Object.seal()”Introduction
Section titled “Introduction”Object.seal() prevents adding or deleting properties but allows modifying existing properties. It’s less restrictive than Object.freeze().
Example
Section titled “Example”const user = Object.seal({ name: 'Alice', age: 25 });
user.age = 26; // ✅ Allowed — modificationuser.email = 'a@b.com'; // ❌ Cannot adddelete user.name; // ❌ Cannot deleteObject.isSealed(user); // truefreeze vs seal
Section titled “freeze vs seal”| Feature | freeze | seal |
|---|---|---|
| Read properties | ✅ | ✅ |
| Modify properties | ❌ | ✅ |
| Add properties | ❌ | ❌ |
| Delete properties | ❌ | ❌ |
Summary
Section titled “Summary”Object.seal()prevents add/delete but allows modification- Use when you want to prevent structural changes but allow value updates
- Check with
Object.isSealed()