Tuesday, 28 August 2018

JavaScript: Working with Infinity

Javascript provides properties Infinity, -Infinity to support mathematical infinity. When the result of any arithmetic operation is greater than the maximum number represented by JavaScript, then it is treated as Infinity. When the result of any arithmetic operation is less than the minimum number represented by JavaScript, then it is treated as -Infinity.

Some points to remember
a. Any positive number divide by zero leads to Infinity.
b. Any negative number divide by zero leads to -Infinity.
c. Any positive number multiplied by Infinity is Infinity
d. Anything divided by Infinity is 0.

Ex:
var a = 10/0;      // Infinity
var b = -10/0;     // -Infinity
var c = 10 * Infinity; // Infinity
var d = 10 * -Infinity; //  -Infinity
var e = 10/Infinity;       // 0
var f = 10/-Infinity;      // 0

infinity.html
<!DOCTYPE html>

<html>

<head>
    <title>Infinity</title>
</head>

<body>
    <script type="text/javascript">
        var a = 10 / 0; // Infinity
        var b = -10 / 0; // -Infinity
        var c = 10 * Infinity; // Infinity
        var d = 10 * -Infinity; //  -Infinity
        var e = 10 / Infinity; // 0
        var f = 10 / -Infinity; // 0

        document.write("a = " + a + "<br />");
        document.write("b = " + b + "<br />");
        document.write("c = " + c + "<br />");
        document.write("d = " + d + "<br />");
        document.write("e = " + e + "<br />");
        document.write("f = " + f + "<br />");
    </script>
</body>

</html>




Previous                                                 Next                                                 Home

No comments:

Post a Comment