ReviseAlgo Logo

Objects & Prototypes

Object Methods

Master JavaScript built-in Object utilities. Compare Object.keys, values, entries, assign, and understand modifications controls with freeze and seal.

Last Updated: July 15, 2026 10 min read

1. Introduction

JavaScript provides static utility methods on the global Object constructor to inspect, copy, and control access permissions to object properties.

2. Why It Matters

Built-in Object utilities are essential for iterating over properties, merging configurations, and securing critical objects from accidental modification or mutation in production.

3. Real-World Analogy

Think of a Legal Document:

  • Object.assign (Photocopy and Paste): Copying data sections from three different reports and pasting them onto a main document.
  • Object.seal (Notarized Contract): You can still edit the values of existing terms (properties), but you cannot add new clauses or delete any existing ones.
  • Object.freeze (Locked Archive Document): A finalized legal decree locked behind protective glass. No one can add, delete, or change any of the text. It is completely read-only.

4. Core Object Methods

Let's explore the primary static methods:

1. Property Enumeration:

  • Object.keys(obj): Returns an array of an object's enumerable property names (keys).
  • Object.values(obj): Returns an array of an object's enumerable property values.
  • Object.entries(obj): Returns an array of key-value pairs as nested arrays: [key, value].

2. Copying & Merging:

Object.assign(target, ...sources): Copies properties from source objects to a target object, returning the target. It performs a shallow copy.

5. Object Integrity & Modification Controls

Method Add Properties? Delete Properties? Modify Property Values?
Standard Object Yes Yes Yes
Object.seal(obj) No No Yes
Object.freeze(obj) No No No

6. Practical Example

This script shows how to prevent mutations on a configuration object in production:

7. Common Mistakes

  • Assuming freeze is deep: Object.freeze only performs a shallow freeze. Nested objects can still be modified. For a deep freeze, you must recursively freeze all nested objects.

8. Quick Quiz

Q1: Which method prevents adding or deleting properties from an object, but allows modifying existing property values?

A) Object.freeze()

B) Object.seal()

Answer: B — Object.seal() allows modifications of existing properties but prevents extensions or deletions.

9. Scenario-Based Challenge

The Deep Freeze Utility:

Write a utility function deepFreeze(obj) that recursively traverses all nested objects and freezes them, ensuring that the entire structure is completely immutable.

10. Debugging Exercise

Explain why this config initialization mutates the source settings:

const defaultSettings = { theme: 'light', debug: false };
const userConfig = { debug: true };

// Objective: Create a new config merging both without mutating defaults const activeConfig = Object.assign(defaultSettings, userConfig); console.log(defaultSettings.debug); // logs true! defaultSettings was mutated!

View Solution

Diagnosis: Object.assign mutates its first argument (the target object). By passing defaultSettings as the first argument, it is modified directly.

Fix: Pass an empty object {} as the target to copy properties without mutating the source objects:

const activeConfig = Object.assign({}, defaultSettings, userConfig); // defaultSettings remains safe

11. Interview Questions

🟢 Q1: Compare Object.freeze() and Object.seal() in terms of property descriptors.

Answer:
Object.seal() sets the configurable descriptor attribute of all properties to false, preventing them from being deleted or reconfigured, but keeps writable: true so values can still be changed.
Object.freeze() sets both the configurable and writable descriptor attributes of all properties to false, preventing any additions, deletions, reconfigurations, or value modifications.

12. Production Considerations

  • Immutable Libraries: For complex, performance-critical applications, consider using specialized libraries like Immer or Immutable.js to manage immutable data structures rather than manually sealing or freezing objects.