About Lesson
Object Methods
JavaScript provides several built-in methods for working with objects.
Object.assign()
This method is used to copy the values of all properties from one or more source objects to a target object.
Syntax:
JavaScript
Object.assign(target, ...sources);
Example:
JavaScript
let target = { a: 1, b: 2 };
let source = { b: 4, c: 5 };
let newObject = Object.assign(target, source);
console.log(newObject); // Output: { a: 1, b: 4, c: 5 }
Object.keys()
This method returns an array of a given object’s own property names.
Example:
JavaScript
let person = { name: "Manjeet", age: 25, city: "Sirsa" };
let keys = Object.keys(person);
console.log(keys); // Output: ["name", "age", "city"]
Object.values()
This method returns an array of a given object’s own property values.
Example:
JavaScript
let values = Object.values(person);
console.log(values); // Output: ["Manjeet", 25, "Sirsa"]