# JS Tip of the Day: Preventing Extensions in Objects

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-preventing-extensions-in-objects/643101>\
**Category:** web dev\
**Created:** [May 5, 2020, 1:32pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-preventing-extensions-in-objects/643101 "2020-05-05T13:32:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [May 5, 2020, 1:32pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-preventing-extensions-in-objects/643101/1 "2020-05-05T13:32:39Z")

</div>

**Preventing Extensions in Objects**  
Level: Beginner

Objects are a collection of properties. Normally you can add new properties to them with dot (or bracket) syntax and the assignment operator.

```javascript
let obj = {};
obj.prop = 'a new property';
console.log(obj.prop); // a new property

```

But you can also lock down an object and prevent new properties from being added by “preventing extensions” in that object. This can be done with the function `Object.preventExtensions()`.

```javascript
let obj = {};
Object.preventExtensions(obj);
obj.prop = 'a new property'; // TypeError in strict mode
console.log(obj.prop); // undefined

```

Preventing extensions does not prevent you from deleting properties, only adding them. It also doesn’t apply to inheritance so a new property could be added if inherited.

Once you prevent extensions in an object, you can’t go back. But you can check to see if an object has had its extensions prevented using `Object.isExtensible()`.

```javascript
let obj = {};
console.log(Object.isExtensible(obj)); // true
Object.preventExtensions(obj);
console.log(Object.isExtensible(obj)); // false

```

More info:

- [preventExtensions on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions)
