Number Data Type in JavaScript

The JavaScript number data type stores the numbers as a double-precision 64-bit number The JavaScript has two data types to deal with the Numbers. One is a primitive number data type. The other one is BigInt, which is a recent addition. It does not have separate data types like integers, decimal, float as in the other languages. The JavaScript also have Number object. We also have listed the difference between number Vs Number. The Number object has many properties & methods, which you can make use of and is listed at the end of the article.

Number Data Type

The number data type represents all numbers in JavaScript. It also contains three special values i.e. Not A Number (NaN), positive infinity and negative infinity. The number is a primitive data type in JavaScript

Defining number

There are two ways you can define a primitive number

Using literal syntax

Number global function

Number object

The JavaScript also has the Number object. The Number object is created using the Number constructor function i.e. by using the new Number() keyword.

If you do not use the new keyword, then a primitive number is created

You can check the type of a variable using the typeof keyword.

number Vs Number

The number is a primitive data type. It is the one you must use. The primitive data type does not have properties or methods.

The Number is a wrapper object around number. It is created by using the syntax new Number(value). The objects have properties & methods.

NaN

The NaN stands for not a number. It is the result of numerical operations, where result is not a number.

For Example:

A non-numeric value as the argument to the Number’s constructor results in NaN.

You can read more about it from NaN in JavaScript tutorial

Max, Min & Safe Values

The JavaScript number has its limitation regarding Max Value, Safe Value & Min Value that you can use. The JavaScript uses the MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, MAX_VALUE & MIN_VALUE to represent these values

The above limitations are due the fact that the Javascript stores the numbers as double-precision 64-bit number. Each number can be stored using the total 64-bit memory. Out of 64bits, one is used for the sign, 11 for the exponent and remaining 52 bits are used for the mantissa (significand).

The max integer that we can store in 52 bits is 9007199254740991. It is represented by MAX_SAFE_INTEGER & the minimum value is -9007199254740991 and is represented by MIN_SAFE_INTEGER

Any number above this number will result in loss of accuracy. For Example, adding 1 & 2 to the MAX_SAFE_INTEGER results in the same value

You can use the Number.isSafeInteger method to check whether the number is safe.

The largest number possible to represent using the number data type is 1.7976931348623157e+308 and it is represented by Number.MAX_VALUE. The lowest number is Number.MIN_VALUE.

The bitwise operators and shift operators operate on 32-bit integers only, so in that case, the max safe integer is 2147483647.

Infinity

The  Infinity is the property of the global Number object.

Infinity can result in many ways, for example multiplying Number.MAX_VALUE by 2 results in infinity.

You can use the Number.isFinite method to verify whether the number is finite.

Exponential Notation

We can use exponential notation e to represent a very large or a very small number. For Example 3×10^9 in Exponential notation is 3e9

Binary & Hexadecimal numbers

To represent the Hexadecimal numbers prefix them with 0x

Syntax to represent the Binary numbers (0b) introduced in ES6. Prior to that the JavaScript didn’t provide any literal form to represent binary numbers

Octal numbers

Since ES6 0o is used to represent the octal number

Invalid numbers throws an error.

But prior to ES6 the octal numbers are prefixed with 0

But invalid octal numbers like 049 are treated as decimal numbers

You can still use the older syntax in ES6. But if you are using strict mode then using the older syntax will throw an error.

Number Properties & Methods

Properties

PropertyDescription
EPSILONThe value of Number.EPSILON is the difference between 1 and the smallest value greater than 1 that is representable as a Number value, which is approximately: 2.2204460492503130808472633361816 x 10‍−‍16.
MAX_SAFE_INTEGERThe value of the largest integer n such that n and n + 1 are both exactly representable as a Number value. The value of Number.MAX_SAFE_INTEGER is 9007199254740991 2^53 − 1.
MAX_VALUEThe value of the largest integer n such that n and n + 1 are both exactly representable as a Number value. The value of Number.MAX_SAFE_INTEGER is 9007199254740991 2^53 − 1.
MIN_SAFE_INTEGERThe value of the largest integer n such that n and n + 1 are both exactly representable as a Number value. The value of Number.MAX_SAFE_INTEGER is 9007199254740991 2^53 − 1.
MIN_VALUEThe closest number to zero that can be represented in JavaScript. Equal to approximately 5.00E-324
NEGATIVE_INFINITYA value that is less than the largest negative number that can be represented in JavaScript. JavaScript displays NEGATIVE_INFINITY values as -infinity.
NaNA value that is not a number. In equality comparisons, NaN does not equal any value, including itself. To test whether a value is equivalent to NaN, use the isNaN function.
POSITIVE_INFINITYA value greater than the largest number that can be represented in JavaScript. JavaScript displays POSITIVE_INFINITY values as infinity.

Methods

MethodDescription
isFiniteReturns true if passed value is finite. Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a number. Only finite values of the type number, result in true.

@param number — A numeric value.
isIntegerReturns true if the value passed is an integer, false otherwise.

@param number — A numeric value.
isNaNReturns a Boolean value that indicates whether a value is the reserved value NaN (not a number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter to a number. Only values of the type number, that are also NaN, result in true.

@param number — A numeric value.
isSafeIntegerReturns true if the value passed is a safe integer.

@param number — A numeric value.
parseIntConverts A string to an integer.

@param s — A string to convert into a number.

@param radix — A value between 2 and 36 that specifies the base of the number in numString. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
parseFloatConverts a string to a floating-point number.

@param string — A string that contains a floating-point number.

Instance Methods

MethodDescription
toExponentialReturns a string containing a number represented in exponential notation.

@param fractionDigits — Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
toFixedReturns a string representing a number in fixed-point notation.

@param fractionDigits — Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
toLocaleStringConverts a number to a string by using the current or specified locale.

@param locales — A locale string or array of locale strings that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

@param options — An object that contains one or more properties that specify comparison options.
toPrecisionReturns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.

@param precision — Number of significant digits. Must be in the range 1 - 21, inclusive.
toStringReturns a string representation of an object.

@param radix — Specifies a radix for converting numeric values to strings. This value is only used for numbers.
valueOfReturns the primitive value of the specified object.

Summary

The JavaScript number is a primitive data type stores the numbers as double-precision 64-bit number. The JavaScript also have Number object, which is a wrapper around primitive number. It is always suggested to use the primitive number. The Number object provides several properties, methods etc.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top