Number prototype Property - Javascript Number

Javascript examples for Number:prototype


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

Number.prototype refers to the Number() object itself.


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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

Number.prototype.myMethod = function() {
    return this.valueOf() / 2;/*from w  ww  . j a  va2s .c  o  m*/

function myFunction() {
    var n = 55;
    document.getElementById("demo").innerHTML = n.myMethod();


Related Tutorials