DEV Community

Cover image for 10 Important JavaScript Object Methods everyone must know in 2023
💡Piyush Kesarwani
💡Piyush Kesarwani

Posted on

10 Important JavaScript Object Methods everyone must know in 2023

JavaScript object methods are actions that can be performed on JavaScript objects. They are functions that are associated with an object and can be invoked using the dot notation or bracket notation on that object. In JavaScript, objects can have properties and methods. Properties are data variables and methods are functions that can be executed by invoking them.

Object methods are used to perform certain actions or operations on an object and/or its properties.

In JavaScript, objects have certain built-in methods that can be used to manipulate and interact with the object’s properties and values. Some of the most commonly used object methods include:

Object Keys

Object.keys(obj): Returns an array of the object's own enumerable properties. The Object.keys() the method returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop. This array only includes properties that are enumerable. Enumerable properties are those properties whose internal enumerable flag is set to true.

For example:

let obj = { name: "John", age: 30, city: "New York" };
let keys = Object.keys(obj);
console.log(keys); // Output: [ "name", "age", "city" ]
Enter fullscreen mode Exit fullscreen mode

Object Values

Object.values(obj): Returns an array of the object's own enumerable property values. The Object.values() the method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in the loop. This array includes only the values of enumerable properties.

For example:

let obj = { name: "John", age: 30, city: "New York" };
let values = Object.values(obj);
console.log(values); // Output: [ "John", 30, "New York" ]
Enter fullscreen mode Exit fullscreen mode

Object Entries

Object.entries(obj): Returns an array of the object's own enumerable property [key, value] pairs. The Object.entries() the method returns an array of a given object's own enumerable property [key, value] pairs, in the same order as that provided by a for...in loop. This array includes only the properties that are enumerable.

For example:

let obj = { name: "John", age: 30, city: "New York" };
let entries = Object.entries(obj);
console.log(entries); // Output: [ ["name", "John"], ["age", 30], ["city", "New York"] ]
Enter fullscreen mode Exit fullscreen mode

Object assign

Object.assign(obj1, obj2, ...): Copies the values of all enumerable own properties from one or more source objects to a target object and returns the target object. This method is used to copy properties from one object to another, it makes a complete copy of all properties, including non-enumerable properties.

For example:

let obj1 = { name: "John" };
let obj2 = { age: 30 };
let obj3 = Object.assign(obj1, obj2);
console.log(obj3); // Output: { name: "John", age: 30 }
Enter fullscreen mode Exit fullscreen mode

Object defineProperty

Object.defineProperty(obj, prop, descriptor): Defines a new property directly on an object, or modifies an existing property on an object, and returns the object. The defineProperty() the method allows you to define a new property on an object, or modify an existing property, and provides a way to set the property's attributes.

For example:

let obj = {};
Object.defineProperty(obj, "name", {
  value: "John",
  writable: true,
  enumerable: true,
  configurable: true
});
console.log(obj.name); // Output: "John"

Enter fullscreen mode Exit fullscreen mode

Object defineProperties

Object.defineProperties(obj, props): Defines new or modifies existing properties directly on an object and returns the object. The defineProperties() the method is similar to the defineProperty() method, but it allows you to define multiple properties at once, using an object that contains the property descriptors.

For example:

let obj = {};
Object.defineProperties(obj, {
  name: {
    value: "John",
    writable: true,
    enumerable: true,
    configurable: true
  },
  age: {
    value: 30,
    writable: true,
    enumerable: true,
    configurable: true
  }
});
console.log(obj.name); // Output: "John"
console.log(obj.age); // Output: 30
Enter fullscreen mode Exit fullscreen mode

Object getOwnPropertyNames

Object.getOwnPropertyDescriptor(obj, prop): Returns a property descriptor for the own property of an object. The getOwnPropertyDescriptor() method returns a property descriptor for an own property (that is, one directly present on an object, not present by virtue of being along an object's prototype chain) of a given object.

For example:

let obj = { name: "John" };
let descriptor = Object.getOwnPropertyDescriptor(obj, "name");
console.log(descriptor); 
// Output: { 
//    value: "John",
//    writable: true,
//    enumerable: true,
//    configurable: true 
// }
Enter fullscreen mode Exit fullscreen mode

Object getOwnPropertyNames

Object.getOwnPropertyNames(obj): Returns an array of all own properties' names (enumerable or non-enumerable) of an object. The getOwnPropertyNames() method returns an array of all properties (enumerable or non-enumerable) found directly on an object.

For example:

let obj = { name: "John", age: 30 };
let properties = Object.getOwnPropertyNames(obj);
console.log(properties); // Output: [ "name", "age" ]
Enter fullscreen mode Exit fullscreen mode

Object getPrototypeOf

Object.getPrototypeOf(obj): Returns the prototype (i.e. the internal [[Prototype]]) of the specified object. The getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.

For example:

let obj = { name: "John" };
let prototype = Object.getPrototypeOf(obj);
console.log(prototype); // Output: {}
Enter fullscreen mode Exit fullscreen mode

Object Create

Object.create(proto[, propertiesObject]): creates an object with the given prototype or with null as a prototype if none is given, and an optional properties object that describes its own properties. The create() method creates a new object with the specified prototype object and properties. If the prototype is not an object or null, it throws a TypeError.

For example:

let prototype = { sayHi: function() { console.log("Hi") } };
let obj = Object.create(prototype);
obj.sayHi(); // Output: "Hi"
Enter fullscreen mode Exit fullscreen mode

These methods allow you to interact with objects in JavaScript in a more powerful way, they allow you to create, modify, and retrieve properties and values of objects, and to manipulate the prototype chain of an object.


That’s a wrap. Thanks for reading.

Follow me for weekly new tidbits on the domain of tech.

Need a Top Rated UI/UX designer and Front-End Development Freelancer to chop away your development woes? Contact me on Upwork and Freelancer

Want to see what I am working on? Check out my Personal Website, Twitter, and GitHub.

Want to connect? Reach out to me on LinkedIn.

Top comments (0)