Search This Blog

Monday, 1 August 2016

How to call Method that display a number's half value in javascript?

Html page:

<button onclick="myFunction()">Try it</button>
<p id="P4"></p>

Javascript page:

<script>
    Number.prototype.myMethod = function () {
        return this.valueOf() / 2;
    };

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

Output:

27.5

No comments:

Post a Comment