Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


Which method can be used to prevent new properties from being added to an object?

  1. Object.freeze()

  2. Object.seal()

  3. Object.assign()

  4. Object.create()

The correct answer is: Object.seal()

The method that can be used to prevent new properties from being added to an object is Object.seal(). When an object is sealed, it means that new properties cannot be added to it, and existing properties cannot be removed. However, the values of existing properties can still be changed. This is particularly useful when you want to ensure that the structure of an object remains intact while still allowing for modifications to existing properties. In contrast, Object.freeze() goes a step further by not only preventing the addition of new properties and the removal of existing ones but also making the existing properties immutable. This means that the values of those properties cannot be modified either, which is a stricter level of protection. Object.assign() is a method used to copy values from one or more source objects to a target object, effectively merging objects but not restricting property additions. Object.create() allows for the creation of a new object with a specified prototype object and properties, but it does not impose any restrictions on adding new properties to the created object itself. Therefore, Object.seal() is the correct choice because it specifically fulfills the requirement of preventing new properties from being added while allowing modifications to existing properties.