JavaScript - getOwnPropertySymbols() method

revision:


Category : object

The Object.getOwnPropertySymbols() static method returns an array of all symbol properties found directly upon a given object.

Syntax :

        Object.getOwnPropertySymbols(obj)
    

Parameters:

obj : required. The object whose symbol properties are to be returned.

Examples:

        const object1 = {};
        const a = Symbol('a');
        const b = Symbol.for('b');

        object1[a] = 'localSymbol';
        object1[b] = 'globalSymbol';

        const objectSymbols = Object.getOwnPropertySymbols(object1);

        console.log(objectSymbols.length);
        // Expected output: 2

    

Practical examples

example: using Object.getOwnPropertySymbols() method

see console.log()

code:
                    <div>
                        <p>see console.log()</p>
                        <p id="symbol-1"></p>
                        <p id="symbol-2"></p>
                        <p id="symbol-3"></p>
                    </div>
                    <script>
                        const obj = {};
                        const a = Symbol("a");
                        const b = Symbol.for("b");
            
                        obj[a] = "localSymbol";
                        obj[b] = "globalSymbol";
            
                        const objectSymbols = Object.getOwnPropertySymbols(obj);
            
                        console.log(objectSymbols.length); // 2
                        console.log(objectSymbols); // [Symbol(a), Symbol(b)]
                        console.log(objectSymbols[0]); // Symbol(a)
                        // document.getElementById("symbol-1").innerHTML = " length of symbols : " + objectSymbols.length; 
                        // document.getElementById("symbol-2").innerHTML = " object symbols : " + toString(objectSymbols); 
                        // document.getElementById("symbol-3").innerHTML = " object symbols : " + (objectSymbols[0]); 
                    </script>
                

example: return an array of all the symbol properties found in a given object.

see console.log()

code:
                    <div>
                        <p>see console.log()</p>
                        <p id="symbol-4"></p>
                        <p id="symbol-5"></p>
                        <p id="symbol-6"></p>
                    </div>
                    <script>
                        const symbol1 = Symbol('symbol1');
                        const symbol2 = Symbol('symbol2');
                        const object = {
                            property1: 'value1',
                            [symbol1]: 'value2',
                            [symbol2]: 'value3'
                        };
                        // get all the symbols of the obj
                        const symbols = Object.getOwnPropertySymbols(object);
                        console.log(symbols); 
                        // Output: [ Symbol(symbol1), Symbol(symbol2) ]
                        // document.getElementById("symbol-4").innerHTML = " symbols : " + symbols;     
                    </script>
                

example: get all symbols of an object.

examples: :

see console.log()

code:
                    <div>
                        <p>see console.log()</p>
                        <p id="symbol-1"></p>
                        
                    </div>
                    <script>
                        // create a symbol id
                        let id = Symbol("id");
                        // create a symbol name
                        let name = Symbol("name");
                        // create an object with
                        // symbol keys: id and name
                        // and string key: age
                        let superhero1 = {
                        [id]: 102,
                        [name]: "Bruce Wayne",
                        age: 40 
                        };
                        // get all symbols of superhero1
                        let objectSymbols1 = Object.getOwnPropertySymbols(superhero1);
                        // print the symbols
                        console.log(objectSymbols1);
                        // Output: [ Symbol(id), Symbol(name) ]
            
                    </script>