The propertyIsEnumerable
function in JavaScript is used to determine whether or not some data element belongs to a particular object and can be enumerated upon using a for
loop.
All objects in JavaScript have the
propertyIsEnumerable
method.
propertyIsEnumerable(property)
The propertyIsEnumerable
function takes in one argument:
property
- the property whose existence and enumerability is to be determined.The propertyIsEnumerable
function returns a boolean value.
If the data element is not present inside the object or the object itself is not enumerable, false
is returned. If both conditions hold, the function returns true
.
For all enumerable properties other than
Symbols
, afor
loop may be used to iterate over them.
The program below declares two objects. The first object is named dict
and contains 3 key-value pairs such that each key is the name of a territory and the corresponding value is the name of its capital city.
The second object is an array, which contains 2 integers.
The propertyIsEnumerable
function is called on both objects multiple times using different arguments, and the return value is printed using the console.log
function.
In the case of arrays, the
propertyIsEnumerable
checks whether or not some value exists at the specified index, returningtrue
if it exists.
const dict = {};const arr = [12, 13];dict.America = "New York City";dict.Japan = "Tokyo";dict.UK = "London";const value=5;console.log(dict.propertyIsEnumerable('America'));// Returns true, since America is present in dict// and dict is enumerableconsole.log(dict.propertyIsEnumerable('Russia'));// Returns false, since Russia is not present in dictconsole.log(arr.propertyIsEnumerable(0));console.log(arr.propertyIsEnumerable(1));// Both return true as values exist at indexes 0 and 1 of arr and arr is enumerableconsole.log(arr.propertyIsEnumerable(2));// Returns false as no value exists at index 2 of arr