JavaScript, always stored the numbers in the form of floating-point or double-precision, and it stored the numbers in 64 bits. In JavaScript numbers are also known as the fractions which are stored in bits from 0 to 51, the sign is stored in 63 bits and the exponent is stored in bits 52 to 62 bits.
In JavaScript a number can be used in the following forms:
Numbers with or without Decimals
To write the numbers in JavaScirpt with or without decimal points i as follows:
let b = 3.55;
How to write extra-large or extra-small numbers in JavaScript
To write extra-large numbers in Javascript the following syntax will be used:
let d = 43e-2 // 0.0043
Integer precision
As we discussed above, in JavaScript numbers are floating-point numbers; therefore, we should also know about the precision of an integer. In JavaScript, an integer is accurate up to 15 digits as shown in the given example.
Example
let b =9999999999999999; // b will be equal to 100000000000000
Floating Precision
Floating precision is also a part of floating-point numbers in JavaScript. However, when we apply some arithmetic operation on floating numbers, their answer will not be accurate. Have a look at the given example.
Example
This problem can be solved by applying the multiplication and division operations on it.
Number’s working with string
In JavaScript if we add a number with a number in string, then instead of addition, concatenation takes place. As shown in the given example.
let b = "45"
c = a+b;
However, if we apply other arithmetic operations on two strings then in resultant we will get numbers instead of a string as shown in the following example.
Symbolic number values
The floating-point numbers further have three types of symbolic values:
- NaN (Not a Number)
- +Infinity Number
- -Infinity Number
NaN (Not a Number)
In JavaScript, if the result of some arithmetic operation is not a number then NaN is returned as shown in the code snippet given below:
Moreover, isNaN() is a global function available in javascript for checking if the value is a number or not, and by default its initial value is “Not-A-Number”. The current browsers do not support this function because it is a non-writable and non-configured function. The following program shows an example of isNaN().
Example
Infinity
When it comes to calculation numbers, javascript has a limit and we can’t more than the largest possible number(1.7976931348623157e+308). Now, any number above than the largest possible number in javascript would be considered as an Infinity.
Let’s divide a number with zero and check the result:
In Javascript, the type of “infinity” is number:
Negative Infinity(-Infinity)
Just like Infinity, any number below than the smallest possible number(5e-324) in javaScript would be considered as a Negative Infinity(-Infinity).
Let’s divide a number with zero and check the result:
Numbers as Object()
In javaScript numbers can also be represented in the form of an object. We can define numbers as object by using the keyword “new”. Take a look at the given example.
Conclusion
JavaScript has only one type of number known as “floating-point numbers” that follows the IEEE 754 standards. In JavaScript numbers are also known as the fractions which are stored in bits from 0 to 51, the sign is stored in 63 bits and the exponent is stored in bits 52 to 62 bits. This post explains how numbers behave with the strings during arithmetic operations, and what are symbolic number values with the help of examples.