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 statement describes the function of Object.freeze() on an object?

  1. Allows changes to existing properties only

  2. Prevents changes to both existing and new properties

  3. Does nothing to the object

  4. Automatically generates new properties

The correct answer is: Prevents changes to both existing and new properties

The function of Object.freeze() is to prevent any modifications to an object. When an object is frozen using this method, it cannot have its existing properties changed, it cannot have new properties added, and it cannot have existing properties removed. This provides a way to create immutable objects in JavaScript, ensuring that the object remains in its initial state throughout its lifecycle. When an object is frozen, it is vital to understand that this action is deep in the sense that it locks down the top-level properties of that object but does not affect objects that are nested within. To make a nested object immutable, each layer would need to be frozen individually. The other options present different scenarios: allowing changes to existing properties, doing nothing at all, or automatically generating new properties, none of which accurately describe the behavior of Object.freeze(). The key aspect of Object.freeze() is its enforcement of immutability on both existing and new properties, making it a powerful tool for safeguarding data integrity within an application.