Essential JavaScript Object Methods: A Comprehensive Guide with Examples
Object Methods
Here’s a list of some commonly used built-in object methods in JavaScript
Object.keys(obj)
- Returns an array of the object's own enumerable property names.
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // Output: ["a", "b", "c"]
Object.values(obj)
- Returns an array of the object's own enumerable property values.
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // Output: [1, 2, 3]
Object.entries(obj)
- Returns an array of the object's own enumerable property [key, value] pairs.
const obj = { a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // Output: [["a", 1], ["b", 2], ["c", 3]]
Object.assign(target, ...sources)
- Copies the values of all enumerable properties from one or more source objects to a target object.
const target = {};
const source = { a: 1, b: 2 };
Object.assign(target, source);
console.log(target); // Output: { a: 1, b: 2 }
Object.defineProperty(obj, prop, descriptor)
- Defines a new property directly on an object or modifies an existing property's characteristics.
const obj = {
prop1: ‘value1',
prop2: ‘value2'
};
// Get the property descriptor of "prop1"
const propertyDescriptor = Object.getOwnPropertyDescriptor(obj, ‘prop1');
console.log(propertyDescriptor);
// Expected Output
{
value: 'value1', // The value of the property
writable: true, // Whether the property can be changed
enumerable: true, // Whether the property will appear in a for...in loop
configurable: true // Whether the property can be deleted or its attributes can be modified
}
Object.getOwnPropertyDescriptor(obj, prop)
- Returns the descriptor for a specific property of an object.
const obj = { a: 1, b: 2 };
Object.freeze(obj);
obj.c = 3; // Attempt to add a new property, but it's ignored in strict mode or throws an error in non-strict mode.
console.log(obj); // Output: { a: 1, b: 2 }
Object.freeze(obj)
- Freezes an object, making it immutable by preventing adding, modifying, or deleting properties.
console.log(Object.is(5, 5)); // Output: true
console.log(Object.is({}, {})); // Output: false (because they are different objects)
Object.seal(obj)
- Seals an object, preventing the addition or deletion of properties but allowing the modification of existing properties.
const obj = { a: 1, b: 2 };
Object.seal(obj);
obj.c = 3; // Attempt to add a new property, but it's ignored in strict mode or throws an error in non-strict mode.
delete obj.a; // Attempt to delete a property, but it's ignored in strict mode or throws an error in non-strict mode.
obj.b = 10; // Existing properties can be modified.
console.log(obj); // Output: { a: 1, b: 10 }
Object.getPrototypeOf(obj)
- Returns the prototype (i.e., the __proto__
) of an object.
const obj = {};
const proto = Object.getPrototypeOf(obj);
console.log(proto === Object.prototype); // Output: true
Object.setPrototypeOf(obj, prototype)
- Sets the prototype (i.e., the __proto__
) of an object.
Object.setPrototypeOf(obj, prototype)
- Sets the prototype (i.e., the __proto__
) of an object.
Object.getOwnPropertyNames(obj)
- Returns an array of all properties (enumerable or non-enumerable) found directly on an object.
const obj = { a: 1, b: 2 };
console.log(obj.hasOwnProperty('a')); // Output: true
console.log(obj.hasOwnProperty('c')); // Output: false
Object.hasOwnProperty(prop)
- Returns a boolean indicating whether an object has a property with the specified name.
Object.hasOwnProperty(prop)
- Returns a boolean indicating whether an object has a property with the specified name.
Object.defineProperty(obj, prop, descriptor)
- Defines a new property directly on an object or modifies an existing property’s characteristics.
const obj = {};
Object.defineProperty(obj, 'name', {
value: 'John',
writable: false, // The property cannot be modified.
enumerable: true, // The property will be included in Object.keys() and Object.values().
configurable: false // The property cannot be deleted or have its attributes changed.
});
console.log(obj.name); // Output: John
obj.name = 'Jane'; // Attempt to modify, but it's ignored in strict mode or throws an error in non-strict mode.
delete obj.name; // Attempt to delete, but it's ignored in strict mode or throws an error in non-strict mode.
Object.create(proto, propertiesObject)
- Creates a new object with the specified prototype object and optional property descriptors.
These are just some of the built-in methods available on the Object
object in JavaScript. Keep in mind that there are many other methods available in the JavaScript ecosystem and you can also define custom methods on your own objects