• JavaScript Video Tutorials

JavaScript - Symbol.description Property



An addition to the JavaScript language, the Symbol.description property was first included in ECMAScript. It allows the description of a Symbol, an immutable and distinct data type first introduced in ES6.

Symbols lacked an intrinsic human-readable representation and were effectively opaque values prior to the introduction of Symbol.description. Developers can give a Symbol a string description by using Symbol.description. The uniqueness of the symbol is unaffected by this description; two separate symbols can have the same description. However, it offers a convenient way of adding meaning or metadata to Symbols, making them more useful in debugging and logging scenarios.

Syntax

Following is the syntax of JavaScript Symbol.description property −

A.description;

Parameters

This property doesn't accept any kind of parameter.

Return value

This property returns the optional description of the symbol object.

Examples

Example 1

Let's look at the following example, where we are going to use the x.description to return the description of the symbol.

<html>
   <style>
      body {
         font-family: Bradley Hand ITC;
         font-size: 30px;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = Symbol('TutorialsPoint');
         document.write(x.description);
      </script>
   </body>
</html>

If we execute the above program, it will displays the text on the webpage.

Example 2

Consider the following example, where we are going to iterate over the symbols defined in th 'tp' object and log their descriptions.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = Symbol('Hi');
         const y = Symbol('Welcome');
         const tp = {
            [x]: 'a',
            [y]: 'b'
         };
         Object.getOwnPropertySymbols(tp).forEach(z => {
            document.write(z.description + " < br > ");
         });
      </script>
   </body>
</html>

On executing the above script, it will displays a text on the webpage.

Example 3

In the following example, we are going to create a function that checks whether symbol contains description or not.

<html>
   <style>
      body {
         font-family: Bradley Hand ITC;
         font-size: 30px;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         function x(sym) {
            return !!sym.description;
         }
         const a = Symbol('Welcome');
         const b = Symbol();
         document.write(x(a) + " < br > ");
            document.write(x(b));
      </script>
   </body>
</html>

When we execute the script, it will displays a text on the webpage.

Advertisements