JavaScript symbol.description property


The symbol.description property in JavaScript converts a Symbol object to a primitive value. The syntax is as follows −

Symbol()[Symbol.toPrimitive](hint);

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Click to display...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
   function display() {
      const val = Symbol('john');
      console.log(val[Symbol.toPrimitive]);
   }
</script>
</body>
</html>

Output

Click the “Result” button −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Click to display...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
   function display() {
      const val = Symbol(2465);
      var res = val[Symbol.toPrimitive](99);
      console.log(res)
   }
</script>
</body>
</html>

Output

Click the “Result” button and get the result in Console −

Updated on: 17-Dec-2019

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements