JavaScript Object getOwnPropertySymbols() Method

The Object.getOwnPropertySymbols() method in JavaScript is a standard built-in object which returns an array of all symbol properties that are present in a given object. An empty array is returned until symbol properties are set on the object.
Syntax:Â
Object.getOwnPropertySymbols(obj)
Parameters:Â
- obj: This parameter is the object whose symbol properties are to be returned.
Return value: This method returns an array of all symbol properties that correspond to the properties found directly in the given object.
Below are some examples of this methodÂ
Example 1:Â In this example, we will check for the symbol properties of the objects using the Object.getOwnPropertySymbols() Method in Javascript.
javascript
<script> Â Â Â Â const object1 = {}; Â Â Â Â let vala = Symbol('geek1'); Â Â Â Â let valb = Symbol.for('geek2'); Â Â Â Â Â Â Â Â Â Â object1[vala] = 'localSymbol'; Â Â Â Â object1[valb] = 'globalSymbol'; Â Â Â Â Â Â Â Â Â Â const objectSymbols = Object.getOwnPropertySymbols(object1); Â Â Â Â console.log(objectSymbols.length); Â Â Â Â Â Â Â Â Â Â const object2 = {};Â Â Â Â Â Â let a = Symbol('a');Â Â Â Â Â Â let b = Symbol.for('b');Â Â Â Â Â Â const objectSymbols1 = Object.getOwnPropertySymbols(object2);Â Â Â Â Â Â console.log(objectSymbols1.length);Â </script> |
Output:Â
2 0
Example 2:Â In this example, we will check for the symbol properties of the objects using the Object.getOwnPropertySymbols() Method in Javascript.
javascript
<script> Â Â Â Â const object1 = {}; Â Â Â Â let vala = Symbol('geek1'); Â Â Â Â let valb = Symbol.for('geek2'); Â Â Â Â let valc = Symbol.for('geek3'); Â Â Â Â Â Â Â Â Â Â object1[vala] = 'localSymbol'; Â Â Â Â object1[valb] = 'globalSymbol'; Â Â Â Â object1[valc] = 'globalSymbol'; Â Â Â Â Â Â Â Â Â Â const objectSymbols = Object.getOwnPropertySymbols(object1); Â Â Â Â console.log(objectSymbols.length); Â Â Â Â console.log(objectSymbols);Â Â Â Â Â Â Â Â Â Â console.log(objectSymbols[0]); Â Â Â Â console.log(objectSymbols[2]); Â Â Â Â console.log(objectSymbols[1]); </script> |
Output:Â
3 Array [Symbol(geek1), Symbol(geek2), Symbol(geek3)] Symbol(geek1) Symbol(geek3) Symbol(geek2)
We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.
Supported Browsers:Â
- Google Chrome 38 and above
- Edge 12 and above
- Firefox 36 and above
- Opera 25 and above
- Safari 9 and above



