Math Methods tutorial in JavaScript

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Math Methods JavaScript</title>
    <script>
        //ceil() function will give upword value
        var a = Math.ceil(5.2);
        document.write(a + "<br>");
        var a2 = Math.ceil(-2.35);
        document.write(a2 + "<br><br>");

        //floor() function will give downword value
        var a3 = Math.floor(2.85);
        document.write(a3 + "<br>");
        var a4 = Math.floor(5.10);
        document.write(a4 + "<br>");
        var a5 = Math.floor(-2.60);
        document.write(a5 + "<br>");
        var a6 = Math.floor(-5.39);
        document.write(a6 + "<br>");
        var a7 = Math.floor(0.60);
        document.write(a7 + "<br><br>");

        //round() function will give nearest integer value
        var a8 = Math.round(0.60);
        document.write(a8 + "<br>");
        var a9 = Math.round(2.40);
        document.write(a9 + "<br>");
        var a10 = Math.round(2.60);
        document.write(a10 + "<br>");
        var a11 = Math.round(2.50);
        document.write(a11 + "<br>");
        var a12 = Math.round(2.49);
        document.write(a12 + "<br><br>");

        //trunc() function will give only integer value. trunc() function will neglect value after point
        var a13 = Math.trunc(2.49);
        document.write(a13 + "<br>");
        var a14 = Math.trunc(8.19);
        document.write(a14 + "<br><br>");

        //max() function will give maximum number
        var a15 = Math.max(8, 10, 2, 50, 25);
        document.write(a15 + "<br>");
        var a16 = Math.min(8, 10, 2, 50, 25);
        document.write(a16 + "<br>");
        var a17 = Math.min(-10, -2);
        document.write(a17 + "<br>");
        var a18 = Math.max(-10, -2);
        document.write(a18 + "<br><br>");

        var a19 = Math.sqrt(4);
        document.write(a19 + "<br>");
        var a20 = Math.sqrt(49);
        document.write(a20 + "<br><br>");

        var a21 = Math.cbrt(125);
        document.write(a21 + "<br>");
        var a22 = Math.cbrt(512);
        document.write(a22 + "<br><br>");

        //pow() function will give power of given number
        var a23 = Math.pow(4, 3);
        document.write(a23 + "<br>");
        var a24 = Math.pow(2., 3);
        document.write(a24 + "<br><br>");

        //random() function will give random value between 0 and 1 by default
        var a25 = Math.random();
        document.write(a25 + "<br>");
        //If we want to take value from 1 to 10 then we have to calculate some mathematical operation
        //as shown below :
        var a26 = (Math.random() * 10) + 1;
        document.write(a26 + "<br>");
        var a27 = Math.floor(Math.random() * 10) + 1;
        document.write(a27 + "<br>");
        //If we want to take value from 1 to 100 then we have to calculate some mathematical operation
        //as shown below :
        var a28 = Math.floor(Math.random() * 100) + 1;
        document.write(a28 + "<br><br>");

        //abs() function will give absolute value
        var a29 = Math.abs(5.25);
        document.write(a29 + "<br>");
        var a30 = Math.abs(-5.25);
        document.write(a30 + "<br>");
        var a31 = Math.abs(null);
        document.write(a31 + "<br>");
        var a32 = Math.abs("Hello");
        document.write(a32 + "<br>");
        var a33 = Math.abs(2 + 3);
        document.write(a33 + "<br><br>");

        //PI will give mathematical constant value 3.14 OR 22/7
        var a34 = Math.PI;
        document.write(a34);
    </script>
</head>

<body>
</body>

</html>



Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript