11 - Numbers in Javascript

There are some operations that come within Javascript itself. They have been written in the form of functions and can be called to perform the operation. These kinds of functions are also called as predefined functions.

Numbers in Javascript

The numbers in Javascript is in the 64 bit floating point numbers. This means that when the number is converted to binary, it can store up to 64 binary digits in length. These 64 bits are divided up for the fraction and the decimal. The first 52 bits are used to store the fraction part or the mantissa. The next 11 bits are used to store the exponent. The last bit is used for storing the sign of the number.

These can be written with or without the decimal point. When writing in the decimal format, it is also possible to write it in the exponential format. The numbers can also be written in octal and hexadecimal format. If the number is preceded by a zero, it is considered octal. If the number is preceded by a zero followed by x, then it is considered to be in hexadecimal format.

var simpleNumber = 2;
var decimalNumber = 3.14;
var negativeNumber = -8;
var negativeDecimal = -9.4;
var exponentialPositive = 43e6;
var exponentialNegative = 754e-23
var octalNumber = 0345;
var hexadecimalNumber = 0x234;

Converting Number to String

The toString() function is used to convert a number to a string. Javascript usually represents numbers in base 10. The toString() function is capable of outputting to other number bases like binary, hexadecimal and octal bases.

<!DOCTYPE html>
<html>
    <head>
        We will test the use of toString() function on Numbers.
        <script type="text/javascript">
        </script>
    </head>
    <hr/>
    <body>
        <script type = "text/javascript">
        var numberToString = 18;
        document.write("The number converted to string in decimal format is " + numberToString.toString() + ".<br/>");
        document.write("The number converted to string in binary format is " + numberToString.toString(2) + ".<br/>");
        document.write("The number converted to string in octal format is " + numberToString.toString(8) + ".<br/>");
        document.write("The number converted to string in hexadecimal format is " + numberToString.toString(16) + ".<br/>");
        </script>
    </body>
</html>


Checking if the value is a number

When we store values as strings, we would need to convert them to numbers to perform numerical operations on them. Before converting one can check with the NaN. NaN is a keyword or a reserved word in Javascript that denotes ‘not a number’. isNaN(value) is a predefined function that returns true if the value passed to the function is not a number and false otherwise.

<!DOCTYPE html>
<html>
    <head>
        We will test the use of isNaN() function.
        <script type="text/javascript">
        </script>
    </head>
    <hr/>
    <body>
        <script type = "text/javascript">
          var isNumber = "18";
          document.write("Is the value stored in isNumber, a number? : " + isNaN(isNumber) + "<br/>");
          var isNotNumber = "a";
          document.write("Is the value stored in isNotNumber, a number? : " + isNaN(isNotNumber) + "<br/>");
        </script>
    </body>
</html>


Infinity in Javascript

Earlier, we saw that numbers are represented in 64 bits. What if we try to store numbers that require more than the possible 64 bits? Then there occurs an overflow. When you try to store a very large positive number beyond the recommended size, it is considered to be a positive overflow. On the negative end, it is called negative overflow. When such an overflow occurs, then Javascript will return ‘Infinity’.

For example let us try to divide a number by zero and print its result. Then we would know how Javascript can handle the situation.

<!DOCTYPE html>
<html>
    <head>
        We will test the use of Infinity.
        <script type="text/javascript">
        </script>
    </head>
    <hr/>
    <body>
        <script type = "text/javascript">
           var numberInfinity = 2/0;
           document.write("The value stored in numberInfinity as a result of divide by zero operation is " + numberInfinity + ".<br/>");
        </script>
    </body>
</html>


Converting to Integer and Floating Point Numbers

This is essential, isn’t it? Earlier, we converted a number to string. Why not try the reverse? That is why we have a couple of functions that can help us convert a string to either a fixed or a floating point number.

parseFloat(string) function

This function can convert a string to a floating point number. The string may have the number as fractional or exponential format. If any other characters occur in the string, then the function converts till that character and skips the rest of the string. If it encounters a non number character in the beginning itself, it returns NaN.

parseInt(string, radix) function

This function is capable of converting a string to an integer. The radix specifies to which base the integer should be represented. If the radix is zero or unspecified, then it assumes that if the number begins with 0, then it is octal and hexadecimal if it begins with ox. If the first character cannot be converted then it returns a NaN.

Like us on Facebook