Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Object.getOwnPropertySymbols() Method

  • Last Updated : 22 Sep, 2021

The Object.getOwnPropertySymbols() method in JavaScript is standard built-in objects which returns an array of all symbol property that are present in a given 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.
Here is the example of this method 
Example 1: 
 

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: 
 

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)

Supported Browsers: 

  • Google Chrome 38 and above
  • Edge 12 and above
  • Firefox 36 and above
  • Opera 25 and above
  • Safari 9 and above

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!