JavaScript(JS) object method - propertyIsEnumerable
The propertyIsEnumerable()
method is a built-in method of the JavaScript Object
constructor. It checks whether a specific property of an object is enumerable or not.
Here's the syntax:
obj.propertyIsEnumerable(prop)
where obj
is the object that we want to check, and prop
is the name of the property to check.
This method returns a boolean value indicating whether the specified property is enumerable or not. If the property is enumerable, propertyIsEnumerable()
returns true
; otherwise, it returns false
.
Here's an example that shows how to use the propertyIsEnumerable()
method:
const myObj = { prop1: "value1", prop2: "value2" }; console.log(myObj.propertyIsEnumerable("prop1")); // true console.log(myObj.propertyIsEnumerable("toString")); // false
In this example, we create an object myObj
with two properties prop1
and prop2
. We use the propertyIsEnumerable()
method to check whether the prop1
property is enumerable or not. Since prop1
is an own and enumerable property of the object, propertyIsEnumerable()
returns true
.
Next, we use the propertyIsEnumerable()
method to check whether the built-in toString()
method of the object is enumerable or not. Since toString()
is not an own property of the object, propertyIsEnumerable()
returns false
.
The propertyIsEnumerable()
method is useful for checking whether a specific property of an object is enumerable or not. Note that this method only checks if a property is enumerable, it does not check if the property exists in the object or if it is inherited from a prototype. For that, you can use the hasOwnProperty()
and in
operators.