Returns an array of a given object's own property names, in the same order as we get with a normal loop.
const obj = { name: 'Alice', age: 25 };
const keys = Object.keys(obj);
console.log(keys); // ['name', 'age']
Returns an array of a given object's own property values, in the same order as provided by a normal loop.
const obj = { name: 'Alice', age: 25 };
const values = Object.values(obj);
console.log(values); // ['Alice', 25]
Returns an array of a given object's own enumerable property [key, value] pairs.
const obj = { name: 'Alice', age: 25 };
const entries = Object.entries(obj);
console.log(entries); // [['name', 'Alice'], ['age', 25]]
Copies all enumerable own properties from one or more source objects to a target object. Returns the target object.
const target = { a: 1 };
const source = { b: 2, c: 3 };
const returnedTarget = Object.assign(target, source);
console.log(returnedTarget); // { a: 1, b: 2, c: 3 }
Freezes an object, preventing new properties from being added, existing properties from being removed or changed.
const frozenObj = Object.freeze({ name: 'Bob' });
frozenObj.name = 'Charlie'; // This won't change the property
console.log(frozenObj.name); // 'Bob'
Seals an object, preventing new properties from being added and marking all existing properties as non-configurable.
const sealedObj = Object.seal({ name: 'Dave' });
sealedObj.age = 30; // This won't add a new property
sealedObj.name = 'Eve'; // This will change the property
console.log(sealedObj); // { name: 'Eve' }
Creates a new object with the specified prototype object and properties. Useful for inheritance.
const proto = { greet() { console.log('Hello!'); } };
const newObj = Object.create(proto);
newObj.greet(); // 'Hello!'
Returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
const sampleObj = { x: 1 };
console.log(sampleObj.hasOwnProperty('x')); // true
console.log(sampleObj.hasOwnProperty('y')); // false
Returns a string representing the object. Useful for checking the type of an object.
console.log(Object.prototype.toString.call([])); // '[object Array]'
console.log(Object.prototype.toString.call({})); // '[object Object]'
Returns the primitive value of the specified object. For most objects, it returns the object itself.
const objValue = { value: 42 };
console.log(objValue.valueOf()); // { value: 42 }
Returns an array of all properties (including non-enumerable ones) found directly in a given object.
const objWithNonEnumerable = Object.create({}, {
nonEnumerableProp: {
value: 'I am non-enumerable',
enumerable: false
}
});
console.log(Object.getOwnPropertyNames(objWithNonEnumerable)); // ['nonEnumerableProp']
Returns all own property descriptors of a given object, providing detailed information about each property.
const descriptorObj = { a: 1 };
const descriptors = Object.getOwnPropertyDescriptors(descriptorObj);
console.log(descriptors); // { a: { value: 1, writable: true, enumerable: true, configurable: true } }