Skip to content

Object Methods

This lesson explains Object Methods in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.

JavaScript Object Methods Overview

JavaScript Object methods are built-in methods used to create, inspect, copy, freeze, seal, compare, group, and manage objects. They help developers work with object properties, key-value pairs, prototypes, descriptors, and object immutability in a clean and reliable way.

Object methods are commonly used in frontend applications, backend APIs, React state management, data transformation, configuration objects, and JavaScript interviews.

JavaScript Object Methods Example

const user = {
  name: "John",
  age: 30,
  role: "Developer"
};

console.log(Object.keys(user));
console.log(Object.values(user));
console.log(Object.entries(user));

const copyUser = Object.assign({}, user);

console.log(copyUser);

This example shows how to get object keys, values, entries, and create a shallow copy using common JavaScript Object methods.

Top 10 JavaScript Object Method Examples

# Method Syntax
1 Get Keys Object.keys({ name: "John", age: 30 })
2 Get Values Object.values({ name: "John", age: 30 })
3 Get Entries Object.entries({ name: "John" })
4 Create From Entries Object.fromEntries([["name", "John"]])
5 Copy Object Object.assign({}, user)
6 Freeze Object Object.freeze(user)
7 Seal Object Object.seal(user)
8 Check Own Property Object.hasOwn(user, "name")
9 Compare Values Object.is(NaN, NaN)
10 Group Data Object.groupBy(users, user => user.role)

JavaScript Object Built-in Methods with Examples and Output

Method Description Example Output
Object.keys() Returns object keys. Object.keys({ name: "John", age: 30 }) ["name", "age"]
Object.values() Returns object values. Object.values({ name: "John", age: 30 }) ["John", 30]
Object.entries() Returns key-value pairs. Object.entries({ name: "John" }) [["name", "John"]]
Object.fromEntries() Creates object from entries. Object.fromEntries([["name", "John"]]) { name: "John" }
Object.assign() Copies properties into target object. Object.assign({}, { a: 1 }, { b: 2 }) { a: 1, b: 2 }
Object.create() Creates object with a prototype. Object.create({ type: "user" }) New object with prototype
Object.freeze() Prevents adding, deleting, or updating properties. Object.freeze({ name: "John" }) Frozen object
Object.isFrozen() Checks if object is frozen. Object.isFrozen(Object.freeze({})) true
Object.seal() Prevents adding or deleting properties. Object.seal({ name: "John" }) Sealed object
Object.isSealed() Checks if object is sealed. Object.isSealed(Object.seal({})) true
Object.preventExtensions() Prevents adding new properties. Object.preventExtensions({ name: "John" }) Non-extensible object
Object.isExtensible() Checks if object can be extended. Object.isExtensible({}) true
Object.hasOwn() Checks if object has its own property. Object.hasOwn({ name: "John" }, "name") true
hasOwnProperty() Checks if property exists directly on object. ({ name: "John" }).hasOwnProperty("name") true
Object.is() Compares two values accurately. Object.is(NaN, NaN) true
Object.getPrototypeOf() Returns object prototype. Object.getPrototypeOf({}) Object.prototype
Object.setPrototypeOf() Sets object prototype. Object.setPrototypeOf(obj, proto) Updated object
Object.defineProperty() Defines one property with descriptor. Object.defineProperty(obj, "id", { value: 1 }) Object with id property
Object.defineProperties() Defines multiple properties. Object.defineProperties(obj, { id: { value: 1 } }) Object with multiple properties
Object.getOwnPropertyDescriptor() Returns descriptor for one property. Object.getOwnPropertyDescriptor(obj, "name") Descriptor object
Object.getOwnPropertyDescriptors() Returns descriptors for all properties. Object.getOwnPropertyDescriptors(obj) Descriptors object
Object.getOwnPropertyNames() Returns own property names. Object.getOwnPropertyNames({ a: 1 }) ["a"]
Object.getOwnPropertySymbols() Returns symbol properties. Object.getOwnPropertySymbols(obj) [Symbol()]
Object.groupBy() Groups items by callback result. Object.groupBy(users, user => user.role) { admin: [...], user: [...] }

Best Practices

  • Use Object.keys() when you need property names.
  • Use Object.values() when you need only values.
  • Use Object.entries() when looping through key-value pairs.
  • Use Object.assign() for shallow copying objects.
  • Use Object.freeze() for constants and configuration objects.
  • Use Object.hasOwn() instead of relying on prototype properties.

Key Takeaways

  • JavaScript Object methods help inspect and manage objects.
  • Object.keys(), Object.values(), and Object.entries() are the most commonly used methods.
  • Object.freeze() and Object.seal() help control object changes.
  • Object.assign() creates a shallow copy.
  • Object methods are important for APIs, React state, Node.js, and interviews.