How to use the Object.keys() method In Javascript
The Object.keys()
method returns an array of a given object’s own enumerable property names. You can then check if the property exists by searching for its name in the array.
const obj = { name: "John", age: 30 };
console.log(Object.keys(obj).includes("name")); // Output: true
console.log(Object.keys(obj).includes("gender")); // Output: false
How to check if an object has a specific property in JavaScript?
In JavaScript, you can check if an object has a specific property using various methods, such as the hasOwnProperty()
method, the in
operator, or using the Object.keys()
method.
Here’s how you can achieve it using these methods:
Table of Content
- Using the hasOwnProperty() method
- Using the in operator
- Using the Object.keys() method
Contact Us