- Object.keys(obj) - Returns an array of an object's keys
const obj = { name: "John", age: 30 };
console.log(Object.keys(obj)); // ["name", "age"]
- Object.values(obj) - Returns an array of an object's values
console.log(Object.values(obj)); // ["John", 30]
- Object.entries(obj) - Returns an array of key-value pairs
console.log(Object.entries(obj)); // [["name", "John"], ["age", 30]]
- Object.assign(target, source) - Copies properties from one object to another
const target = { a: 1 };
const source = { b: 2 };
Object.assign(target, source);
console.log(target); // { a: 1, b: 2 }
- Object.freeze(obj) - Freezes an object (prevents modification)
const obj2 = { name: "Alice" };
Object.freeze(obj2);
obj2.name = "Bob"; // No effect
console.log(obj2.name); // "Alice"
- Object.seal(obj) - Prevents adding/removing properties but allows modification
const obj3 = { age: 25 };
Object.seal(obj3);
obj3.age = 26; // Allowed
delete obj3.age; // Not allowed
console.log(obj3); // { age: 26 }
- Object.create(proto) - Creates a new object with a specified prototype
const parent = { greet: function() { return "Hello"; } };
const child = Object.create(parent);
console.log(child.greet()); // "Hello"
- Object.getPrototypeOf(obj) - Returns the prototype of an object
console.log(Object.getPrototypeOf(child) === parent); // true
- Object.setPrototypeOf(obj, proto) - Sets the prototype of an object
const newProto = { sayHi: () => "Hi!" };
Object.setPrototypeOf(child, newProto);
console.log(child.sayHi()); // "Hi!"
- Object.hasOwn(obj, prop) - Checks if an object has a property (ES2022+)
console.log(Object.hasOwn(obj, "name")); // true
- Object.hasOwnProperty(prop) - Checks if an object has a property (older method)
console.log(obj.hasOwnProperty("age")); // true
- Object.getOwnPropertyNames(obj) - Returns an array of all property names
console.log(Object.getOwnPropertyNames(obj)); // ["name", "age"]
- Object.getOwnPropertyDescriptors(obj) - Returns property descriptors
console.log(Object.getOwnPropertyDescriptors(obj));
- Object.defineProperty(obj, key, descriptor) - Defines a new property
Object.defineProperty(obj, "gender", { value: "male", writable: false });
console.log(obj.gender); // "male"
- Object.defineProperties(obj, props) - Defines multiple properties
Object.defineProperties(obj, {
country: { value: "USA", writable: true },
hobby: { value: "Reading", writable: false }
});
console.log(obj.country, obj.hobby); // "USA" "Reading"
- Object.is(value1, value2) - Compares two values (like === but handles NaN correctly)
console.log(Object.is(NaN, NaN)); // true