0 day streak
javascript / intermediate
Snippet

Preventing Property Additions with Object.seal()

Object.seal() locks an object's structure. It prevents the addition or deletion of properties but still allows you to modify the values of existing properties.

snippet.js
javascript
1
2
3
4
5
6
const config = { api: 'v1', timeout: 5000 };
Object.seal(config);
config.timeout = 3000; // Success: Values can be changed
config.token = 'secret'; // Failure: No new properties allowed
delete config.api; // Failure: Properties cannot be removed
Breakdown
1
Object.seal(config);
Seals the object, preventing schema changes.
2
config.timeout = 3000;
Updating existing properties remains functional.