Object.freeze()

Object.freeze() prevents an object from being modified by freezing it. Both the properties and values of the existing object remain intact without any change. The result is the same object is outputted that was passed to the freeze() method. The object is not copied.

Examples:

Adding a property

const person = {
    name: 'Peter',
    age: 47
};

Object.freeze(person);

person.gender = 'Male';

console.log(person); // { name: 'Peter', age: 47 }

Removing a property

const person = {
    name: 'Peter',
    age: 47
};

Object.freeze(person);

delete person.age;

console.log(person); // { name: 'Peter', age: 47 }

Modifying a property

const person = {
    name: 'Peter',
    age: 47
};

Object.freeze(person);

person.age = 44;

console.log(person); // { name: 'Peter', age: 47 }

NOTE: When attempting to modify an object after it has been frozen, all errors will fail silently since strict mode is not turned on. If strict mode is turned on, a TypeError will be raised.

Using the previous example with strict mode enabled:

`use strict';

const person = {
    name: 'Peter',
    age: 47
};

Object.freeze(person);

person.age = 44; //TypeError: Cannot assign to read only property 'age' of object '#<Object>'

Object.freeze() can only freeze properties that are directly part of the main object. Any subsequent objects or arrays that are nested in the first object will not be frozen.

Freezing an array

'use strict';

const array = [1, 2, 3, 4];

Object.freeze(array);

array.push(5);

console.log(array); //TypeError: Cannot add property 4, object is not extensible

Checking if an object has been frozen

We can use the isFrozen() method in order to check whether an object is frozen or not. It will return a boolean.

const person = {
    name: 'Peter',
    age: 47
};

Object.freeze(person);

console.log(Object.isFrozen(person)); //true

Object.seal()

Object.seal() is the opposite of Object.freeze(). It allows you modify existing properties within an object, but you cannot add or delete properties that are directly part of the main object.

Examples:

const person = {
    name: 'Peter',
    age: 47
};

Object.seal(person);

person.age = 44;
person.gender = 'Male';
delete person.age

console.log(person); //{ name: 'Peter', age: 44 }

Add an element to an object within the original object

const person = {
    name: 'Peter',
    age: 47,
    address: {
        number: 123,
        street: 'Anywhere St.',
        city: 'Somewhere',
        state: 'CA',
    },
    deptIds: [1, 2, 3, 4]
};
Object.seal(person);
person.address.country = 'US';
console.log(person); /*{
                            name: 'Peter',
                            age: 47,
                            address: {
                                number: 123,
                                street: 'Anywhere St.',
                                city: 'Somewhere',
                                state: 'CA',
                                country: 'US'
                            },
                            deptIds: [ 1, 2, 3, 4 ]
                       }
                     */

Delete an element to an object within the original object

const person = {
    name: 'Peter',
    age: 47,
    address: {
        number: 123,
        street: 'Anywhere St.',
        city: 'Somewhere',
        state: 'CA',
    },
    deptIds: [1, 2, 3, 4]
};
Object.seal(person);
delete person.address.number;
console.log(person); /*
                       {
                            name: 'Peter',
                            age: 47,
                            address: { street: 'Anywhere St.', city: 'Somewhere', state: 'CA' },
                            deptIds: [ 1, 2, 3, 4 ]
                       }
                     /*
Using strict
'use strict';

const person = {
    name: 'Peter',
    age: 47,
    address: {
        number: 123,
        street: 'Anywhere St.',
        city: 'Somewhere',
        state: 'CA',
    },
    deptIds: [1, 2, 3, 4]
};
Object.seal(person);
delete person.age;
console.log(person); //TypeError: Cannot delete property 'age' of #<Object>

Add an element to an array in an object

const person = {
    name: 'Peter',
    age: 47,
    deptIds: [1, 2, 3, 4]
};

Object.seal(person);

person.deptIds.push(5);

console.log(person); //{ name: 'Peter', age: 47, deptIds: [ 1, 2, 3, 4, 5 ] };

Delete an element of an array in an object

const person = {
    name: 'Peter',
    age: 47,
    address: {
        number: 123,
        street: 'Anywhere St.',
        city: 'Somewhere',
        state: 'CA',
    },
    deptIds: [1, 2, 3, 4]
};
Object.seal(person);
person.deptIds.pop() //Remove the last element of the array deptIds
console.log(person); /*
                        {
                          name: 'Peter',
                          age: 47,
                          address: {
                              number: 123,
                              street: 'Anywhere St.',
                              city: 'Somewhere',
                              state: 'CA'
                          },
                          deptIds: [ 1, 2, 3 ]
                        }

                     */

Check if an object is sealed

const person = {
    name: 'Peter',
    age: 47,
    address: {
        number: 123,
        street: 'Anywhere St.',
        city: 'Somewhere',
        state: 'CA',
    },
    deptIds: [1, 2, 3]
};
Object.seal(person);

console.log(Object.isSealed(person)); //true