A Number is a primitive data type in JavaScript. Both integer and floating-point number formats are supported by JavaScript that can be represented in decimal, hexadecimal or octal notation. JavaScript does not treat integer and floating-point numbers differently, unlike other languages. Numbers in JavaScript are represented as floating-point numbers.
In JavaScript, numbers can also be represented in hexadecimal notation (base 16). These hexadecimal numbers are prefixed with 0x. They are commonly used to represent colors.
Infinite means a number is too big to handle for JavaScript. JavaScript provides special keyword Infinity(∞) and -Infinity(-∞) to represent positive and negative infinity respectively.
For example, dividing to any number by 0 returns infinity as shown below:
Note: Infinity is a special value and it represents the mathematical Infinity ∞, which is greater than any number. The typeof operator returns the number for an Infinity value.
Avoiding Precision Problems:
Sometimes while performing operations on floating-point numbers produce unexpected results, as shown below:
In the above example, the result is 0.30000000000000004 rather than the expected 0.3. This difference in the result is called representation error or roundoff error. This error occurs because JavaScript and many other languages use binary (base 2) form to represent decimal (base 10) numbers internally. But, most decimal fractions can’t be represented exactly in binary form that’s why small differences occur.
To avoid such problem you can use the solution given below:
In JavaScript floating-point numbers are rounded to 17 digits which is enough accuracy in most cases. Also, JavaScript integers are accurate up to 15 digits, as shown in the following example:
JavaScript provides different methods and properties to perform operations on numeric values. As we know in JavaScript primitive data types can act like objects when you refer to them with the property access notation i.e dot notation.
In the following section, we will learn the most commonly used Javascript number methods.
Parsing Integers from Strings:
The parseInt() method is used to parse an integer from a string. If this method encounters a character that is not numeric in the specified base, it stops parsing and returns the integer value parsed up to that point. If the first character cannot be converted into a number, the parseInt() method will return NaN (not a number).
Similarly, the parseFloat() method can be used to parse floating-point numbers from a string. This method works the same way as the parseInt() method, the only difference is that it retrieves both integers and numbers with decimals.
The toString() method is used to convert a number to its string equivalent.
This method accepts an integer parameter optionally in the range 2 through 36 specifying the base to use for representing numeric values.
The following example shows the number to string conversion.
The toExponential() method is used to format or represent a number in exponential notation.
The toExponential() method optionally accepts an integer parameter specifying the number of digits after the decimal point. Also, the returned value is a string, not a number.
Note: Exponential notation is used to represent the numbers that are either very large or very small in magnitude. For example, 12300000000 can be written as 123e+8 or 1.23e+10.
Formatting Numbers to Fixed Decimals:
The toFixed() method is used when you want to format a number with a fixed number of digits to the right of the decimal point. The value returned by toFixed() method is a string and it has exactly specified the number of digits after the decimal point. If the digits parameter is not specified then it is treated as 0.
The toPrecision() method is used when you want the most appropriate form of a number. The toPrecision() method is used to return a string representing the number to the specified precision.
Finding the Largest and Smallest Possible Numbers:
The Number.MAX_VALUE and Number.MIN_VALUE properties of the number object are used to represent the largest and smallest (closest to zero, not most negative) possible positive numbers that JavaScript can handle.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok