What are Object methods and how to use them

· Category: JavaScript

Short answer

Object provides static methods to inspect, copy, seal, freeze, and define properties on objects, giving you fine-grained control over structure and mutability.

Steps

  1. List keys, values, and entries: javascript Object.keys(obj); // array of keys Object.values(obj); // array of values Object.entries(obj); // array of [key, value]
  2. Merge objects: javascript Object.assign(target, source1, source2);
  3. Prevent modifications: javascript Object.freeze(obj); // no changes at all Object.seal(obj); // allow value changes, no add/remove Object.preventExtensions(obj); // no new properties
  4. Define a property with metadata: javascript Object.defineProperty(obj, "id", { value: 1, writable: false, enumerable: true, configurable: false });

Tips

  • Use Object.fromEntries to rebuild an object from an array of pairs.
  • Object.freeze is shallow; nested objects remain mutable unless recursively frozen.

Common issues

  • Object.assign mutates the first argument; pass an empty object to create a new one.
  • Property descriptors default to false for writable, enumerable, and configurable.