Object methods in JavaScript

Object methods in JavaScript are built-in functions that can be called on objects to perform specific operations or access specific information.

For example, managing user profiles in a web application. You can use object methods to create, update, and retrieve user information, such as storing user data as an object, using methods like Object.assign to update profile details, and Object.keys or Object.values to retrieve specific information from the user object. Let’s have a look at some of the Object methods in javascript briefly:

Object.Freeze:

The Object.freeze() method freezes an object i.e. it prevents the object from being modified. Freezing an object does not allow new properties to be added to the object and prevents removing or altering the existing properties. Object.freeze() preserves the enumerability, configurability, writability, and prototype of the object. It returns the passed object and does not create a frozen copy.\

Example:

let obj = {
  prop: function () {},
  foo: "bar",
};

// freeze the object
Object.freeze(obj)

// changes will not occur
obj.foo = "bar1";
console.log(obj.foo);

// Output: bar

Object.keys:

Object.keys method returns an array containing all the keys of the objects.

The syntax of the keys() method is:

Object.keys(obj)

The keys() method, being a static method, is called using the Object class name.

Example:

let Student = {
  name: "Lisa",
  age: 24,
  marks: 78.9,
};

// get all keys of Student
let std1 = Object.keys(Student);
console.log(std1);

// Output: [ 'name', 'age', 'marks' ]

Object.values:

Object.values return the value in the form of an array. JavaScript object.values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually if a loop is applied to the properties. Object.values() takes the object as an argument of which the enumerable own property values are to be returned and returns an array containing all the enumerable property values of the given object.

Syntax:

Object.values(obj)

Parameters:

  • obj: It is the object whose enumerable property values are to be returned.

Return Value: Object.values() returns an array containing all the enumerable property values of the given object.

Example:

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.values(object1));
// Expected output: Array ["somestring", 42, false]

Object.entries:

JavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually. In short Object.entries returns both: the keys and its values in the form of a nested array.

Example:

const obj = { name: "Adam", age: 20, location: "Nepal" };

// returns properties in key-value format
console.log(Object.entries(obj)); 

// Output:  [ [ 'name', 'Adam' ], [ 'age', 20 ], [ 'location', 'Nepal' ] ]

Object.assign:

The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target. It returns the target object which has properties and values copied from the source object. Object.assign() does not throw on null or undefined source values.

Syntax:

Object.assign(target, ...sources)

Example:

let widget = {
    color: 'red'
};

let clonedWidget = Object.assign({}, widget);

console.log(clonedWidget);

{ color: 'red' }

I hope you guys find this tutorial helpful, if you have any questions or any points that you want to add please share them in the comment box. If you like this tutorial please share it with your friends and bookmark this site for more amazing tutorials. Thanks