JavaScript Object Methods (Detailed Explanation)

Learn about the built-in methods of the Object class in JavaScript with definitions, meanings, and examples.

🔹 What are Object Methods?

In JavaScript, Object methods are built-in functions that allow developers to work with objects effectively. They help in retrieving keys, values, entries, assigning properties, preventing modifications, and much more.

1. Object.keys()

Definition: Returns an array of a given object's own enumerable property names (keys).

Meaning: Helps you extract all the keys from an object for iteration or logic building.

// Example:
const student = { name: "Ravi", age: 21, course: "BCA" };
console.log(Object.keys(student));
// Output: ["name", "age", "course"]
    

2. Object.values()

Definition: Returns an array of an object's own enumerable property values.

Meaning: Useful when you need all the values without caring about the keys.

// Example:
console.log(Object.values(student));
// Output: ["Ravi", 21, "BCA"]
    

3. Object.entries()

Definition: Returns an array of an object's own enumerable [key, value] pairs.

Meaning: Handy for iterating both keys and values together.

// Example:
console.log(Object.entries(student));
// Output: [["name","Ravi"], ["age",21], ["course","BCA"]]
    

4. Object.assign()

Definition: Copies the values of all enumerable own properties from one or more source objects to a target object.

Meaning: Used for cloning or merging objects.

// Example:
const details = { city: "Lucknow" };
const merged = Object.assign({}, student, details);
console.log(merged);
// Output: {name: "Ravi", age: 21, course: "BCA", city: "Lucknow"}
    

5. Object.freeze()

Definition: Freezes an object, preventing new properties from being added or existing properties from being changed.

Meaning: Used to make an object immutable.

// Example:
Object.freeze(student);
student.age = 22; // ignored
console.log(student.age); // 21
    

6. Object.seal()

Definition: Prevents new properties from being added, but existing properties can still be modified.

Meaning: Useful when you want to restrict object structure but allow updates.

// Example:
const teacher = { name: "Arun", subject: "Math" };
Object.seal(teacher);
teacher.subject = "Physics"; // allowed
teacher.age = 40; // not allowed
console.log(teacher);
// Output: {name: "Arun", subject: "Physics"}
    

7. Object.create()

Definition: Creates a new object with the specified prototype object.

Meaning: Used for inheritance and prototype-based object creation.

// Example:
const person = { greet() { console.log("Hello!"); } };
const newPerson = Object.create(person);
newPerson.greet(); // Hello!
    

8. Object.hasOwnProperty()

Definition: Returns a boolean indicating whether the object has the specified property as its own property.

Meaning: Helps in checking property existence without checking the prototype chain.

// Example:
console.log(student.hasOwnProperty("name")); // true
console.log(student.hasOwnProperty("toString")); // false
    

9. Object.getOwnPropertyNames()

Definition: Returns an array of all properties (including non-enumerable) found directly in an object.

Meaning: Unlike Object.keys(), it includes hidden properties too.

// Example:
console.log(Object.getOwnPropertyNames(student));
// Output: ["name","age","course"]