JavaScript symbol.valueOf() function


The JavaScript symbol.valueOf() function returns the primitive value of a symbol object.

Following is the code for implementing symbol.valueOf() function −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   div {
      font-size: 20px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript symbol.valueOf() function</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to get the value of symbols
</h3>
<script>
   let fillEle = document.querySelector(".sample");
   const symbol = Symbol('computer');
   const symbol1 = Symbol(234);
   let res = symbol.valueOf();
   let res1 = symbol1.valueOf();
   document.querySelector(".Btn").addEventListener("click", () => {
      fillEle.innerHTML += 'res = ' + res.toString() + "<br>";
      fillEle.innerHTML += 'res1 = ' + res1.toString() + "<br>";
   });
</script>
</body>
</html>

Output

On clicking the “CLICK HERE” button −

Updated on: 11-May-2020

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements