Javascript Reference - JavaScript Number valueOf() Method








The valueOf() method returns the primitive value of a number.

The valueOf() method returns the primitive numeric value represented by the object.


var numberObject = new Number(10); 

console.log(numberObject.valueOf());

The code above generates the following result.

Browser Support

valueOf Yes Yes Yes Yes Yes




Syntax

number.valueOf()

Parameters

None

Return Value

A Number type value representing the primitive value of a number.

Example

The following code shows how to return the primitive value of a number.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--  w w w  . ja  v  a2s .  c  om-->
<p id="demo"></p>

<script>
function myFunction() {
    var num = 15;
    var n = num.valueOf()
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

The code above is rendered as follows: