Tuesday, 28 August 2018

JavaScript: Working with numbers

Javascript don't have specific type for integers and float values. All the numbers are of type ‘number’. Internally these numbers represented in double precision. Javascript number takes 8 bytes memory.

var a = 10; //typeof a return number
var b = 10.02;     //typeof b return number

Different ways to represent numbers
In addition to representing numbers in decimal form, you can alslo represent numbers in octal, hexa and scientific notations.

Octal notation
Numbers in octal notation represented by 0 followed by the number.

Ex:
0123 is equal to 83 in decimal

Hexa notation
In Hexa form number is preceded by ox.

Ex:
0x53 is equal to 83 in decimal.

Scientific notation
You can also represent numbers in scientific notation.

Syntax
[digits][.digits][(E|e)[(+|-)]digits]

Ex:
8.3e1 = 8.3 + (10 power 1) = 83
0.83e2 = 0.83 * (10 power 2) = 83

numbers.html
<!DOCTYPE html>

<html>

<head>
    <title>Numbers</title>
</head>

<body>
    <script type="text/javascript">
        var a = 10; //typeof a return number
        var b = 10.02; //typeof b return number

        var c = 83;
        var d = 0123; // 83 in octal
        var e = 0x53; // 83 in hexa
        var f = 8.3e1; // 83 in scientific notation

        document.write("a = " + a + ", type = " + typeof(a) + "<br />");
        document.write("b = " + b + ", type = " + typeof(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>

Note

ECMAScript standard don’t support numbers in octal form, so better not to use them in your scripts.


Previous                                                 Next                                                 Home

No comments:

Post a Comment