The propertyIsEnumerable()
method of Object
instances returns a boolean indicating whether the specified property is this object's enumerable own property.
The propertyIsEnumerable()
method of Object
instances returns a boolean indicating whether the specified property is this object's enumerable own property.
propertyIsEnumerable(prop)
A boolean value indicating whether the specified property is enumerable and is the object's own property.
All objects that inherit from Object.prototype
(that is, all except null
-prototype objects) inherit the propertyIsEnumerable()
method. This method determines if the specified property, string or symbol, is an enumerable own property of the object. If the object does not have the specified property, this method returns false
.
This method is equivalent to Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false
.
The following example shows the use of propertyIsEnumerable()
on objects and arrays.
const o = {}; const a = []; o.prop = "is enumerable"; a[0] = "is enumerable"; o.propertyIsEnumerable("prop"); // true a.propertyIsEnumerable(0); // true
Most built-in properties are non-enumerable by default, while user-created object properties are often enumerable, unless explicitly designated otherwise.
const a = ["is enumerable"]; a.propertyIsEnumerable(0); // true a.propertyIsEnumerable("length"); // false Math.propertyIsEnumerable("random"); // false globalThis.propertyIsEnumerable("Math"); // false
Only enumerable own properties cause propertyIsEnumerable()
to return true
, although all enumerable properties, including inherited ones, are visited by the for...in
loop.
const o1 = { enumerableInherited: "is enumerable", }; Object.defineProperty(o1, "nonEnumerableInherited", { value: "is non-enumerable", enumerable: false, }); const o2 = { // o1 is the prototype of o2 __proto__: o1, enumerableOwn: "is enumerable", }; Object.defineProperty(o2, "nonEnumerableOwn", { value: "is non-enumerable", enumerable: false, }); o2.propertyIsEnumerable("enumerableInherited"); // false o2.propertyIsEnumerable("nonEnumerableInherited"); // false o2.propertyIsEnumerable("enumerableOwn"); // true o2.propertyIsEnumerable("nonEnumerableOwn"); // false
Symbol
properties are also supported by propertyIsEnumerable()
. Note that most enumeration methods only visit string properties; enumerability of symbol properties is only useful when using Object.assign()
or spread syntax. For more information, see Enumerability and ownership of properties.
const sym = Symbol("enumerable"); const sym2 = Symbol("non-enumerable"); const o = { [sym]: "is enumerable", }; Object.defineProperty(o, sym2, { value: "is non-enumerable", enumerable: false, }); o.propertyIsEnumerable(sym); // true o.propertyIsEnumerable(sym2); // false
Because null
-prototype objects do not inherit from Object.prototype
, they do not inherit the propertyIsEnumerable()
method. You must call Object.prototype.propertyIsEnumerable
with the object as this
instead.
const o = { __proto__: null, enumerableOwn: "is enumerable", }; o.propertyIsEnumerable("enumerableOwn"); // TypeError: o.propertyIsEnumerable is not a function Object.prototype.propertyIsEnumerable.call(o, "enumerableOwn"); // true
Alternatively, you may use Object.getOwnPropertyDescriptor()
instead, which also helps to distinguish between non-existent properties and actually non-enumerable properties.
const o = { __proto__: null, enumerableOwn: "is enumerable", }; Object.getOwnPropertyDescriptor(o, "enumerableOwn")?.enumerable; // true Object.getOwnPropertyDescriptor(o, "nonExistent")?.enumerable; // undefined
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
propertyIsEnumerable |
1 | 12 | 1 | 4 | 3 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable