for...in
Loop
Description: Iterates over all enumerable properties of an object.
Use: Useful for iterating through the properties of an object.
Example:
for (const key in dog) {
console.log(key, dog[key]);
}
// Output: name Max, age 6, color black
Object.keys()
Description: Returns an array of a given object's own enumerable property names.
Use: Useful for getting all the keys of an object.
Example:
let keys = Object.keys(dog);
console.log(keys); // Output: [ 'name', 'age', 'color' ]
for...of
Loop
Description: Iterates over the values of an iterable object.
Use: Useful for iterating through arrays or other iterable objects.
Example:
for (const key of keys) {
console.log(key, dog[key]);
}
// Output: name Max, age 6, color black
Object.values()
Description: Returns an array of a given object's own enumerable property values.
Use: Useful for getting all the values of an object.
Example:
let values = Object.values(dog);
console.log(values); // Output: [ 'Max', 6, 'black' ]
Object.entries()
Description: Returns an array of a given object's own enumerable property [key, value] pairs.
Use: Useful for getting both keys and values of an object.
Example:
let entries = Object.entries(dog);
console.log(entries); // Output: [ [ 'name', 'Max' ], [ 'age', 6 ], [ 'color', 'black' ] ]
Object.getOwnPropertyDescriptors()
Description: Returns all own property descriptors of an object.
Use: Useful for getting detailed information about an object's properties.
Example:
let descriptors = Object.getOwnPropertyDescriptors(dog);
console.log(descriptors);
// Output: { name: { value: 'Max', writable: true, enumerable: true, configurable: true }, age: { value: 6, writable: true, enumerable: true, configurable: true }, color: { value: 'black', writable: true, enumerable: true, configurable: true } }
Object.getOwnPropertySymbols()
Description: Returns an array of all symbol properties found directly on a given object.
Use: Useful for working with symbol properties of an object.
Example:
let symbols = Object.getOwnPropertySymbols(dog);
console.log(symbols); // Output: []
Object.freeze()
Description: Freezes an object, preventing new properties from being added, existing properties from being removed, and existing properties from being changed.
Use: Useful for making an object immutable.
Example:
Object.freeze(dog);
dog.name = 'Maxwell';
console.log(dog); // Output: { name: 'Max', age: 6, color: 'black' }
Object.seal()
Description: Seals an object, preventing new properties from being added or existing properties from being removed. Existing properties can still be modified.
Use: Useful for partially making an object immutable.
Example:
Object.seal(dog);
dog.breed = 'German Shepherd';
console.log(dog); // Output: { name: 'Max', age: 6, color: 'black' }
Object.preventExtensions()
Description: Prevents new properties from being added to an object.
Use: Useful for ensuring no new properties can be added to an object.
Example:
Object.preventExtensions(dog);
dog.breed = 'German Shepherd';
console.log(dog); // Output: { name: 'Max', age: 6, color: 'black' }
Object.isFrozen()
Description: Determines if an object is frozen.
Use: Useful for checking if an object is immutable.
Example:
let isFrozen = Object.isFrozen(dog);
console.log(isFrozen); // Output: true
Object.isSealed()
Description: Determines if an object is sealed.
Use: Useful for checking if an object is partially immutable.
Example:
let isSealed = Object.isSealed(dog);
console.log(isSealed); // Output: true
Object.isExtensible()
Description: Determines if an object is extensible (i.e., if new properties can be added to it).
Use: Useful for checking if new properties can be added to an object.
Example:
let isExtensible = Object.isExtensible(dog);
console.log(isExtensible); // Output: false
Object.assign()
Description: Copies all enumerable own properties from one or more source objects to a target object.
Use: Useful for merging objects or cloning an object.
Example:
let assign = Object.assign({}, dog);
console.log(assign); // Output: { name: 'Max', age: 6, color: 'black' }
Object.defineProperty()
Description: Defines a new property directly on an object or modifies an existing property on an object.
Use: Useful for adding or modifying properties with specific descriptors.
Example:
Object.defineProperty(dog, 'breed', {
value: 'German Shepherd',
writable: true,
enumerable: true,
configurable: true
});
console.log(dog); // Output: { name: 'Max', age: 6, color: 'black', breed: 'German Shepherd' }
Object.defineProperties()
Description: Defines new or modifies existing properties directly on an object, returning the object.
Use: Useful for defining multiple properties with specific descriptors at once.
Example:
Object.defineProperties(dog, {
breed: {
value: 'German Shepherd',
writable: true,
enumerable: true,
configurable: true
},
weight: {
value: '100 lbs',
writable: true,
enumerable: true,
configurable: true
}
});
console.log(dog); // Output: { name: 'Max', age: 6, color: 'black', breed: 'German Shepherd', weight: '100 lbs' }