Javascript Reference - JavaScript Number prototype Property








The prototype constructor allows you to add new properties and methods to JavaScript numbers.

Browser Support

prototype Yes Yes Yes Yes Yes

Syntax

Number.prototype.name =value

Example

The following code shows how to Create a new number method that returns a number's half value.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from  w  ww  .ja  va2s. c o  m-->
<script>
Number.prototype.myMethod = function() {
    return this.valueOf() / 2;
}
function myFunction() {
    var n = 50;
    document.getElementById("demo").innerHTML = n.myMethod();
}
</script>

</body>
</html>

The code above is rendered as follows: