JavaScript Object Methods Example.
Object.keys(obj): Returns an array of an object's own enumerable property names (keys).
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.keys(obj));
// Output: ['a', 'b', 'c']
Enter fullscreen mode
Exit fullscreen mode
Object.values(obj): Returns an array of the object's own enumerable property values.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj));
// Output: [1, 2, 3]
Enter fullscreen mode
Exit fullscreen mode
Object.entries(obj): Returns an array of the object's own enumerable string-keyed property [key, value] pairs.
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.entries(obj));
// Output: [['a', 1], ['b', 2], ['c', 3]]
Enter fullscreen mode
Exit fullscreen mode
Object.isSealed(obj): Returns true if the object is sealed, otherwise false.
const obj = Object.seal({ a: 1 });
console.log(Object.isSealed(obj));
// Output: true
Enter fullscreen mode
Exit fullscreen mode
Object.assign(target, source): Copies the values of all enumerable properties from one or more source objects to a target object. It returns the target object.
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = Object.assign(target, source);
console.log(result);
// Output: { a: 1, b: 2, c: 3 }
Enter fullscreen mode
Exit fullscreen mode
Object.freeze(obj): Freezes an object, preventing new properties from being added or existing properties from being removed or reconfigured.
const obj = { name: 'Khabib' };
Object.freeze(obj);
obj.name = 'Bob'; // This won't change the value
console.log(obj.name); // Output: 'Khabib'
Enter fullscreen mode
Exit fullscreen mode
Object.seal(obj): Seals an object, preventing new properties from being added, but allowing existing properties to be modified.
const obj = { name: 'Alice' };
Object.seal(obj);
obj.name = 'Bob'; // This will update the value
obj.age = 25; // This won't add a new property
console.log(obj); // Output: { name: 'Bob' }
Enter fullscreen mode
Exit fullscreen mode
Object.create(proto): Creates a new object with the specified prototype object and properties.
const person = {greet() {console.log('Hello!');}};
const student = Object.create(person);
student.greet();
// Output: 'Hello!'
Enter fullscreen mode
Exit fullscreen mode
Object.defineProperty(obj, prop, descriptor): Defines a new property directly on an object or modifies an existing property.
const obj = {};
Object.defineProperty(obj, 'name', {
value: 'Alice',
writable: false });
console.log(obj.name); // 'Alice'
Enter fullscreen mode
Exit fullscreen mode
Object.defineProperties(obj, props): Defines multiple new properties or modifies existing properties on an object.
const obj = {};
Object.defineProperties(obj, {
name: { value: 'Cormier', writable: false },
age: { value: 30, writable: true } });
console.log(obj.name); // 'Cormier'
Enter fullscreen mode
Exit fullscreen mode
Object.isExtensible(obj): Determines if an object is extensible (i.e., whether new properties can be added).
const obj = {};
console.log(Object.isExtensible(obj)); // true
Object.preventExtensions(obj);
console.log(Object.isExtensible(obj)); // false
Enter fullscreen mode
Exit fullscreen mode
Object.isFrozen(obj): Determines if an object is frozen (i.e., not extensible and all properties are non-writable).
const obj = Object.freeze({ name: 'Gregor' });
console.log(Object.isFrozen(obj));
// output: true
Enter fullscreen mode
Exit fullscreen mode
Object.hasOwn(obj, prop): Returns true if the specified object has the specified property as its own property, even if the property's value is undefined.
const obj = { name: 'Alice' };
console.log(Object.hasOwn(obj, 'name')); // true
console.log(Object.hasOwn(obj, 'age')); // false
Enter fullscreen mode
Exit fullscreen mode
Object.hasOwnProperty(prop): Determines if an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
const obj = { name: 'Alice' };
console.log(obj.hasOwnProperty('name')); // true
console.log(obj.hasOwnProperty('age')); // false
Enter fullscreen mode
Exit fullscreen mode
Object.preventExtensions(obj): Prevents new properties from ever being added to an object.
const obj = {};
Object.preventExtensions(obj);
obj.name = 'Khabib'; // Won't be added
console.log(obj); // {}
Enter fullscreen mode
Exit fullscreen mode
Object.setPrototypeOf(obj, proto): Sets the prototype (the internal [[Prototype]] property) of a specified object.
const proto = { greet() {console.log('Hello!');}};
const obj = {};
Object.setPrototypeOf(obj, proto);
obj.greet(); // 'Hello!'
Enter fullscreen mode
Exit fullscreen mode
Object.fromEntries(iterable): Transforms a list of key-value pairs into an object.
const entries = [['name', 'Rock'], ['age', 35]];
const obj = Object.fromEntries(entries);
console.log(obj); // { name: 'Rock', age: 35 }
Enter fullscreen mode
Exit fullscreen mode
Object.getPrototypeOf(obj) : Returns the prototype (the internal [[Prototype]] property) of the specified object.
const obj = {};
const proto = Object.getPrototypeOf(obj);
console.log(proto === Object.prototype); // true
Enter fullscreen mode
Exit fullscreen mode
Object.getOwnPropertySymbols(obj): Returns an array of all symbol properties found on the object.
const symbol = Symbol('id');
const obj = { [symbol]: 123 };
const symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols); // [Symbol(id)]
console.log(obj[symbols[0]]); // 123
Enter fullscreen mode
Exit fullscreen mode
Object.getOwnPropertyDescriptor(obj, prop): Returns a property descriptor for a specific property of a given object.
const obj = { name: 'Alice', age: 26 };
const descriptor = Object.getOwnPropertyDescriptor(obj, 'name');
console.log(descriptor);
// Output: { configurable: true, enumerable: true, value: "Alice", writable: true }
Enter fullscreen mode
Exit fullscreen mode
Object.getOwnPropertyNames(obj): Returns an array of all properties found on the object (including non-enumerable properties).
const obj = { name: 'Ferguson', age: 30 };
const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames); // ['name', 'age']
Enter fullscreen mode
Exit fullscreen mode
Object.is(value1, value2): Compares if two values are the same.
console.log(Object.is('foo', 'foo')); // true
console.log(Object.is({}, {})); // false
Enter fullscreen mode
Exit fullscreen mode
Object.getOwnPropertyDescriptors(obj): Returns all own property descriptors of an object.
const obj = { name: 'Khabib', age: 28 };
const descriptors = Object.getOwnPropertyDescriptors(obj);
console.log(descriptors);
// Output: {
age: {
configurable: true,
enumerable: true,
value: 28,
writable: true },
name: {
configurable: true,
enumerable: true,
value: "Khabib",
writable: true
}
}
Enter fullscreen mode
Exit fullscreen mode