JavaScript - propertyIsEnumerable() method

revision:


Category : object

The propertyIsEnumerable() method returns a boolean indicating whether the specified property is the object's enumerable own property..

Syntax :

        propertyIsEnumerable(prop)
    

Parameters:

prop : required. The name of the property to test. Can be a string or a Symbol.

Examples:

        const object1 = {};
        const array1 = [];
        object1.property1 = 42;
        array1[0] = 42;

        console.log(object1.propertyIsEnumerable('property1'));
        // Expected output: true

        console.log(array1.propertyIsEnumerable(0));
        // Expected output: true

        console.log(array1.propertyIsEnumerable('length'));
        // Expected output: false
    

Practical examples

example: using propertyIsEnumerable() method.

code:
                    <div>
                        <p id="prop-1"></p>
                        <p id="prop-2"></p>
                    </div>
                    <script>
                        const o = {};
                        const a = [];
                        o.prop = "is enumerable";
                        a[0] = "is enumerable";
            
                        o.propertyIsEnumerable("prop"); // true
                        a.propertyIsEnumerable(0); // true
                        document.getElementById("prop-1").innerHTML = " is enumerable ? : " + o.propertyIsEnumerable("prop");
                        document.getElementById("prop-2").innerHTML = " is enumerable ? : " + a.propertyIsEnumerable(0);
                    </script>
                

example: user-defined vs. built-in objects.

code:
                    <div>
                        <p id="prop-3"></p>
                        <p id="prop-4"></p>
                        <p id="prop-5"></p>
                        <p id="prop-6"></p>
                    </div>
                    <script>
                        const aa = ["is enumerable"];
                        aa.propertyIsEnumerable(0); // true
                        aa.propertyIsEnumerable("length"); // false
                        document.getElementById("prop-3").innerHTML = " is enumerable ? : " + aa.propertyIsEnumerable(0);
                        document.getElementById("prop-4").innerHTML = " is enumerable ? : " + aa.propertyIsEnumerable("length");
            
                        Math.propertyIsEnumerable("random"); // false
                        globalThis.propertyIsEnumerable("Math"); // false
                        document.getElementById("prop-5").innerHTML = " is enumerable ? : " + Math.propertyIsEnumerable("random");
                        document.getElementById("prop-6").innerHTML = " is enumerable ? : " + globalThis.propertyIsEnumerable("Math");
            
                    </script>
                

example: direct vs. inherited properties.

code:
                    <div>
                        <p id="prop-7"></p>
                        <p id="prop-8"></p>
                        <p id="prop-9"></p>
                        <p id="prop-10"></p>
                    </div>
                    <script>
                        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
                        document.getElementById("prop-7").innerHTML = " is enumerable ? : " + o2.propertyIsEnumerable("enumerableInherited");
                        o2.propertyIsEnumerable("nonEnumerableInherited"); // false
                        document.getElementById("prop-8").innerHTML = " is enumerable ? : " + o2.propertyIsEnumerable("nonEnumerableInherited");
                        o2.propertyIsEnumerable("enumerableOwn"); // true
                        document.getElementById("prop-9").innerHTML = " is enumerable ? : " + o2.propertyIsEnumerable("enumerableOwn");
                        o2.propertyIsEnumerable("nonEnumerableOwn"); // false
                        document.getElementById("prop-10").innerHTML = " is enumerable ? : " + o2.propertyIsEnumerable("nonEnumerableOwn");      
                    </script>