Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript Math Object

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 124
    Comment on it

    This object is used to perform mathematical tasks. It has various mathmatical methods. Using this object we can also create the random numbers.
    1)Math.random()
    Example :

    <html>
    <body>
    <p>Math.random() returns a random number between 0 and 1.</p>
    <button onclick="rand()">clicke to see random numbers</button>
    <p id="demo"></p>
    <script>
    function rand() {
        document.getElementById("demo").innerHTML = Math.random();
    }
    </script>
    </body>
    </html>
    

    Output :
    Math.rand() returns a random number between 0 and 1.
    // after click on this button output will be
    0.49024432711303234


    2) Math.min() and Math.max() : This is the second method of math object. Math.min method return the min value among the numbers while Math.max() return the maximum number.
    Example :Math.min()

    <!DOCTYPE html>
    <html>
    <body>
    <p>Math.min()</p>
    <button onclick="minfunction()">click to see minimum numbers</button>
    <p id="demo"></p>
    <script>
    function minfunction() {
        document.getElementById("demo").innerHTML =
        Math.min(0, 150, 30, 20, -8, -200);
    }
    </script>
    </body>
    </html>
    

    Output :
    Math.min() returns a minimum value among the number
    // after click on this button output will be
    -200

    Example :Math.max()

    <!DOCTYPE html>
    <html>
    <body>
    <p>Math.max() returns the lowest value.</p>
    <button onclick="maxfunction()">click to see maximum numbers</button>
    <p id="demo"></p>
    <script>
    function maxfunction() {
        document.getElementById("demo").innerHTML =
        Math.min(0, 150, 30, 20, -8, -200);
    }
    </script>
    </body>
    </html>
    

    Output :
    Math.max() returns a maximum value among the number
    // after click on this button output will be
    150

    3)Math.round() : It help the users to rounds a number to the nearest integer.
    Example :

     <!DOCTYPE html>
    <html>
    <body>
    <p>Math.round()</p>
    <button onclick="roundfunction()">Click to see number, after roundoff</button>
    <p id="demo"></p>
    <script>
    function roundfunction() {
        document.getElementById("demo").innerHTML = Math.round(4.4);
    }
    </script>
    </body>
    </html>
    

    Output :

    4

    4)Math.ceil() : It help the users to rounds a number up to the nearest integer.
    Example :

     <!DOCTYPE html>
    <html>
    <body>
    <p>Math.ceil()</p>
    <button onclick="ceilfunction()">Click to see output</button>
    <p id="demo"></p>
    <script>
    function ceilfunction() {
        document.getElementById("demo").innerHTML = Math.ceil(4.4);
    }
    </script>
    </body>
    </html>
    

    Output :

    5

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: