JavaScript Object.getOwnPropertySymbols()

The Object.getOwnPropertySymbols() method returns an array of all the symbol properties found in a given object.

Example


getOwnPropertySymbols() Syntax

The syntax of the getOwnPropertySymbols() method is:

Object.getOwnPropertySymbols(obj)

Here, getOwnPropertySymbols() is a static method. Hence, we need to access the method using the class name, Object.


getOwnPropertySymbols() Parameters

The getOwnPropertySymbols() method takes in:

  • obj - the object whose symbol properties are to be returned.

getOwnPropertySymbols() Return Value

The getOwnPropertySymbols() method returns an array of all symbol properties found in the given object.

Note: Object.getOwnPropertySymbols() returns all symbol properties of the object while Object.getOwnPropertyNames() returns the string properties.


Example: JavaScript Object.getOwnPropertySymbols()

In the above program, we have created an object superhero1 with the following properties:

  • symbols - id and name
  • string - age

Then, we used the getOwnPropertySymbols() method on superhero1 to list its symbol properties.

As expected, we get an array listing only the symbols id and name as the output.


Recommended Reading: