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.
valueOf |
Yes | Yes | Yes | Yes | Yes |
number.valueOf()
None
A Number type value representing the primitive value of a number.
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: