getOwnPropertySymbols() method - JavaScript Tutorial

getOwnPropertySymbols() method

BLUF: This tutorial on getOwnPropertySymbols() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: getOwnPropertySymbols() method

Understanding getOwnPropertySymbols() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The Object.getOwnPropertySymbols function produces an array containing all symbol properties that are directly associated with a specified object. If there are no symbol properties defined on your object, this method will yield an empty array.

Syntax:

Example

Object.getOwnPropertySymbols(obj)

Parameter

obj : This refers to an object from which the symbol attributes are intended to be retrieved.

Return value:

This function provides an array containing all symbol properties that are directly associated with the specified object.

Browser Support:

Chrome 38
Edge Yes
Firefox 36
Opera 35

Example 1

Example

const object1 = {};
 a = Symbol('a');
 b = Symbol.for('b');
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);

Output:

Example 2

Example

const object1 = {};
 a = Symbol('a');
 b = Symbol.for('b');
object1[a] = 'Carry';
object1[b] = 'Marry';
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);

Output:

Example 3

Example

const object1 = {};
const a = Symbol('a');
const b = Symbol.for('b');
object1[a] = 'localSymbol';
object1[b] = 'globalSymbol';
const object2 = {};
const c = Symbol('c');
object2[c] = 'Carry';
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience