DEV Community

Web Easly
Web Easly

Posted on

2

JavaScript Object Methods

JavaScript, known for its versatility and flexibility, empowers developers with a rich set of methods to manipulate objects. Objects in JavaScript are collections of key-value pairs, where methods play a crucial role in performing actions on these objects.

Let's delve into some essential object methods in JavaScript:

1. Object.keys()

This method extracts keys from an object and returns them as an array. It's particularly useful for iterating through an object's properties or obtaining a list of keys.

Example:

const person = {
  name: 'Alice',
  age: 30,
  occupation: 'Engineer'
};

const keys = Object.keys(person);
console.log(keys); // Output: ['name', 'age', 'occupation']
Enter fullscreen mode Exit fullscreen mode

2. Object.values()

Similar to Object.keys(), this method retrieves the property values of an object and returns them as an array.

Example:

const values = Object.values(person);
console.log(values); // Output: ['Alice', 30, 'Engineer']
Enter fullscreen mode Exit fullscreen mode

3. Object.entries()

This method returns an array containing arrays of key-value pairs from an object. Each inner array comprises a key-value pair.

Example:

const entries = Object.entries(person);
console.log(entries);
// Output: [['name', 'Alice'], ['age', 30], ['occupation', 'Engineer']]
Enter fullscreen mode Exit fullscreen mode

4. Object.hasOwnProperty()

This method checks whether an object has a specific property. It returns true if the object contains the specified property, otherwise false.

Example:

console.log(person.hasOwnProperty('name')); // Output: true
console.log(person.hasOwnProperty('address')); // Output: false
Enter fullscreen mode Exit fullscreen mode

5. Object.assign()

Used for copying the values of all enumerable own properties from one or more source objects to a target object. It's often employed for object cloning or merging.

Example:

const details = {
  country: 'USA',
  hobby: 'Reading'
};

const updatedPerson = Object.assign({}, person, details);
console.log(updatedPerson);
// Output: { name: 'Alice', age: 30, occupation: 'Engineer', country: 'USA', hobby: 'Reading' }
Enter fullscreen mode Exit fullscreen mode

6. Object.freeze() and Object.seal()

Object.freeze() prevents any modifications to an object, including adding or removing properties and modifying existing ones. Object.seal() prevents new properties from being added or removed but allows modifying existing ones.

Example:

const frozenPerson = Object.freeze(person);
// Trying to modify frozenPerson will not work

const sealedPerson = Object.seal(person);
// Existing properties can be modified but not added or removed
Enter fullscreen mode Exit fullscreen mode

These methods form the backbone of object manipulation in JavaScript, enabling developers to efficiently work with and manage objects in various scenarios.

Remember, mastering these methods empowers you to wield the full potential of JavaScript objects, making your code more robust and flexible.

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay