JS Object Methods

Javascript

Common Methods

Here are some of the most useful object methods in JavaScript:

  • Object.keys: Get an array of property names. let obj = {a:1, b:2}; let keys = Object.keys(obj); // ["a", "b"]
  • Object.values: Get an array of property values. let values = Object.values(obj); // [1, 2]
  • Object.entries: Get an array of [key, value] pairs. let entries = Object.entries(obj); // [["a",1], ["b",2]]
  • Object.assign: Copy properties from one or more objects to another. let target = {}; Object.assign(target, {a:1}, {b:2}); // target = {a:1, b:2}
  • Object.hasOwn: Check if an object has a property (own property only). Object.hasOwn({a:1}, "a"); // true
  • Object.freeze: Make an object immutable. let frozen = Object.freeze({a:1}); frozen.a = 2; // No effect

Sources