/

Exploring the `propertyIsEnumerable()` Method in JavaScript

Exploring the propertyIsEnumerable() Method in JavaScript

In JavaScript, the propertyIsEnumerable() method is used to determine whether a specific property of an object is enumerable or not. This method is called on an object instance and accepts a string argument representing the name of the property.

The propertyIsEnumerable() method returns true if the property exists in the object and is enumerable. On the other hand, if the property does not exist or is not enumerable, it returns false.

Here’s an example to demonstrate the usage of the propertyIsEnumerable() method:

1
2
3
4
5
6
7
8
9
const person = { name: 'Fred' }

Object.defineProperty(person, 'age', {
value: 87,
enumerable: false
})

person.propertyIsEnumerable('name') // true
person.propertyIsEnumerable('age') // false

In the above example, we have an object person with two properties: name and age. The name property is enumerable, while the age property is not. When we call the propertyIsEnumerable() method on the person object, it returns true for the name property and false for the age property.

By using the propertyIsEnumerable() method, you can easily check if a specific property of an object is enumerable or not, and make decisions based on the result.

Tags: JavaScript, object, property, enumerable, method