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.

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.

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.

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.

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.