Skip to content

JavaScript Object seal() | Method

  • by

JavaScript Object seal() Method is used to seal an object, preventing new properties from being added to it. And also existing properties from being deleted or their attributes (except for their value) from being changed.

Object.seal(obj)

This prevents the addition of new properties and deletion of existing properties, while still allowing modification of the existing property values.

JavaScript Object seal() example

Simple example code.

<!DOCTYPE html>
<html>
<body>
    <script>
    const person = {
        name: 'John',
        age: 30
    };

    Object.seal(person);

    person.name = 'Jane'; // allowed
    person.city = 'New York'; // not allowed

    delete person.age; // not allowed

    console.log(person); // { name: 'Jane', age: 30 }

    </script>
</body>
</html>

Output:

JavaScript Object seal() Method

This means that you can still change the values of the existing properties, but you cannot modify their attributes such as making them non-writable or non-configurable.

Do comment if you have any doubts or suggestions on this Js object methods topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *