Home >>JavaScript String Methods >JavaScript String valueOf() Method

JavaScript String valueOf() Method

JavaScript String valueOf() Method

JavaScript valueOf() method is used to return the primitive value of a given input String object. It is usually called automatically by JavaScript and not explicitly in code.

Syntax:
string.valueOf()

Browser Support

Method Chrome Edge Firefox Safari Opera
valueOf() Yes Yes Yes Yes Yes
Here is an example of valueOf() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "PHPTPOINT";
  var res = str.valueOf();
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var str = "Abhimanyu";
  var res = str.valueOf();
  document.getElementById("demo1").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.


No Sidebar ads