JavaScript object methods are used to manipulate and access properties of JavaScript objects in various ways. This is a list of common JavaScript object methods, including descriptions and examples of their usage.
objectName.methodName()
Object.keys()
– returns an array of a given object’s enumerable property names.Object.values()
– returns an array of a given object’s enumerable property values.Object entries()
– returns an array of a given object’s enumerable property pairs in the form of[key, value]
.Object.assign()
– copies the values of all enumerable own properties from one or more source objects to a target object.Object.freeze()
– freezes an object: other code can’t delete or change any properties.Object.seal()
– seals an object, preventing new properties from being added and marking all existing properties as non-configurable.Object.create()
– creates a new object with the specified prototype object and properties.Object.defineProperty()
– adds a new property or modifies an existing property on an object, and specifies if the property can be changed or deleted.Object.hasOwnProperty()
– returns a boolean indicating whether the object has the specified property as its property (not inherited).Object.is()
– determines whether two values are the same value, and returns true or false.
Note: there are many more methods available on specific objects, such as Array
or String
.
JavaScript object methods list examples
Simple example code of some common JavaScript object methods.
<!DOCTYPE html>
<html>
<body>
<script>
// Object.keys()
const myObj = {a: 1, b: 2, c: 3};
const keysArray = Object.keys(myObj);
console.log(keysArray);
// Object.values()
const myObj = {a: 1, b: 2, c: 3};
const valuesArray = Object.values(myObj);
console.log(valuesArray); // Output: [1, 2, 3]
// Object.entries()
const myObj = {a: 1, b: 2, c: 3};
const entriesArray = Object.entries(myObj);
console.log(entriesArray);
// Object.assign()
const obj1 = {a: 1, b: 2};
const obj2 = {c: 3, d: 4};
const newObj = Object.assign({}, obj1, obj2);
console.log(newObj);
// Object.freeze()
const myObj = {a: 1, b: 2, c: 3};
Object.freeze(myObj);
myObj.a = 5;
console.log(myObj);
// Object.seal()
const myObj = {a: 1, b: 2, c: 3};
Object.seal(myObj);
myObj.d = 4; // This will have no effect
delete myObj.a; // This will have no effect
console.log(myObj);
// Object.create()
const myObj = Object.create({a: 1, b: 2});
myObj.c = 3;
console.log(myObj.a); // 1
console.log(myObj.b); // 2
console.log(myObj.c); // 3
// Object.defineProperty()
const myObj = {};
Object.defineProperty(myObj, 'a', {
value: 1,
writable: false
});
myObj.a = 2; // This will have no effect
console.log(myObj.a);
// Object.hasOwnProperty()
const myObj = {a: 1, b: 2};
console.log(myObj.hasOwnProperty('a'));
console.log(myObj.hasOwnProperty('c'));
// Object.is()
console.log(Object.is(1, '1')); // false
console.log(Object.is(1, 1)); // true
console.log(Object.is(-0, 0)); // false
console.log(Object.is(NaN, NaN)); // true (weird, right?)
</script>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this JS object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version