The Typescript number data type stores the numbers as a double-precision 64-bit number The Typescript 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 number is a primitive data type. The Typescript also has a 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.

Table of Contents

## Number Data Type

The number data type represents all numbers in TypeScript. 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 TypeScript

There are two ways you can define a **primitive number**

### Using literal syntax

1 2 3 | let numVal=100; //creates a primitive number |

### Number global function

1 2 3 | let numVal=Number(100); //creates a primitive number |

## Number object

The Typescript also has the `Number`

object. The Number object is created using the **Number constructor** i.e by using the `new Number()`

keyword.

1 2 3 4 5 6 7 | let num = new Number("100"); console.log(num) //[Number: 100] //***output****** //[Number: 100] |

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

is created

1 2 3 4 5 6 7 8 9 10 11 12 | let numObject = new Number("100"); let numVal = Number("100"); console.log(numObject) //object output [Number: 100] console.log(numVal) //primitive output 100 //**** output **** //[Number: 100] //100 |

You can check the type of a variable using the `typeof`

keyword.

1 2 3 4 5 6 7 8 9 10 11 12 | let numValue=100; let numObject = new Number(1500); console.log(typeof numValue) //number console.log(typeof numObject) //object //****Output**** //number //object |

### 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.

You can assign primitive `number`

to `Number`

object, but not the other way around.

1 2 3 4 5 6 7 8 9 10 11 12 | let numValue=100; //primitive number let numObject = new Number(1500); //Number object numValue=numObject; //Compile Error numObject=numValue; //ok ***** Error ***** Type 'Number' is not assignable to type 'number'. 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.ts(2322) |

## NaN

The `NaN`

stands for **n**ot **a** **n**umber. It is the result of numerical operations, where the result is not a number.

For Example:

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

.

1 2 3 4 5 6 7 | console.log(Number("test")); console.log(0/0) //***output*** //NaN |

You can read more about it from NaN in Typescript tutorial

## Max, Min & Safe Values

The Typescript `number`

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

The above limitations are due to 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 can be stored in 52 bits is 9007199254740991. It is represented by MAX_SAFE_INTEGER & the minimum value is -9007199254740991 and is represented by MIN_SAFE_INTEGER

1 2 3 4 5 6 7 8 9 | console.log(Number.MAX_SAFE_INTEGER) //9007199254740991 console.log(Number.MIN_SAFE_INTEGER) //-9007199254740991 *** output **** 9007199254740991 -9007199254740991 |

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

1 2 3 4 5 6 7 8 | console.log(Number.MAX_SAFE_INTEGER+1) console.log(Number.MAX_SAFE_INTEGER+2) //*** output ** //9007199254740992 //9007199254740992 |

You can use the `Number.isSafeInteger`

method to check whether the `number`

is safe.

1 2 3 4 5 6 7 8 | console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER)); console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER+1)); //*** output ** //true //false |

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`

.

1 2 3 4 5 6 7 8 | console.log(Number.MAX_VALUE) console.log(Number.MIN_VALUE) ****** Output ***** 1.7976931348623157e+308 5e-324 |

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

## Infinity

The

is the property of the global Number object.**Infinity**

1 2 3 4 5 6 7 8 | console.log(Number.POSITIVE_INFINITY); console.log(Number.NEGATIVE_INFINITY); //** output ** //Infinity //-Infinity |

Infinity can result in many ways, for example multiplying `Number.MAX_VALUE`

by 2 results in `infinity`

.

1 2 3 4 5 6 7 8 9 | let bigNumber=Number.MAX_VALUE * 2; console.log(bigNumber); console.log(bigNumber === Number.POSITIVE_INFINITY) //*** output *** //Infinity //true |

You can use the `Number.isFinite`

method to verify whether the `number`

is finite.

1 2 3 4 5 6 7 8 | console.log(Number.isFinite(Number.POSITIVE_INFINITY)); console.log(Number.isFinite(100)); //***output ** //false //true |

## Number Properties & Methods

### Properties

Property | Description |
---|---|

EPSILON | The 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_INTEGER | The 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_VALUE | The 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_INTEGER | The 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_VALUE | The closest number to zero that can be represented in JavaScript. Equal to approximately 5.00E-324 |

NEGATIVE_INFINITY | A value that is less than the largest negative number that can be represented in JavaScript. JavaScript displays NEGATIVE_INFINITY values as -infinity. |

NaN | A 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_INFINITY | A value greater than the largest number that can be represented in JavaScript. JavaScript displays POSITIVE_INFINITY values as infinity. |

### Methods

Method | Description |
---|---|

isFinite | Returns 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. |

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

isNaN | Returns 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. |

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

parseInt | Converts 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. |

parseFloat | Converts a string to a floating-point number. @param string — A string that contains a floating-point number. |

### Instance Methods

Method | Description |
---|---|

toExponential | Returns 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. |

toFixed | Returns 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. |

toLocaleString | Converts 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. |

toPrecision | Returns 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. |

toString | Returns a string representation of an object. @param radix — Specifies a radix for converting numeric values to strings. This value is only used for numbers. |

valueOf | Returns the primitive value of the specified object. |

## Summary

The Typescript `number`

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