Learn about the built-in methods of the Object class in JavaScript with definitions, meanings, and examples.
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.
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"]
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"]
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"]]
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"}
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
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"}
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!
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
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"]