Skip to content

TypeScript - Numbers

The number type in TypeScript represents numeric values. Unlike some other languages that have distinct types for integers (like int) and floating-point numbers (like float or double), TypeScript’s number type encompasses both.

Internally, it corresponds to JavaScript’s standard double-precision 64-bit floating-point format (IEEE 754).

let integerValue: number = 42;
let floatValue: number = 3.14159;
let hexValue: number = 0xf00d; // Hexadecimal
let binaryValue: number = 0b1010; // Binary
let octalValue: number = 0o744; // Octal
let bigIntValue: bigint = 9007199254740991n; // Note: bigint is a separate type
console.log(integerValue); // 42
console.log(floatValue); // 3.14159
console.log(hexValue); // 61453
console.log(binaryValue); // 10
console.log(octalValue); // 484
console.log(bigIntValue); // 9007199254740991n

While you typically work with primitive number values, JavaScript (and thus TypeScript) also has a built-in Number object constructor. This object provides useful static properties related to numbers:

PropertyDescription
Number.MAX_VALUEThe largest positive representable number.
Number.MIN_VALUEThe smallest positive representable number (closest to zero).
Number.NaNRepresents “Not-a-Number” - result of invalid math operations (e.g., 0 / 0).
Number.NEGATIVE_INFINITYSpecial value representing negative infinity.
Number.POSITIVE_INFINITYSpecial value representing positive infinity.
Number.EPSILON(ES6) Difference between 1 and the smallest floating point number greater than 1.
Number.MAX_SAFE_INTEGER(ES6) Maximum integer safely representable without precision loss (2^53 - 1).
Number.MIN_SAFE_INTEGER(ES6) Minimum integer safely representable without precision loss (-(2^53 - 1)).
console.log("TypeScript Number Properties:");
console.log(`Max Value: ${Number.MAX_VALUE}`);
console.log(`Min Value: ${Number.MIN_VALUE}`);
console.log(`Negative Infinity: ${Number.NEGATIVE_INFINITY}`);
console.log(`Positive Infinity: ${Number.POSITIVE_INFINITY}`);
console.log(`NaN: ${Number.NaN}`);
console.log(`Max Safe Integer: ${Number.MAX_SAFE_INTEGER}`);

NaN is a special numeric value indicating an unrepresentable result. A key characteristic is that NaN is not equal to anything, including itself (NaN === NaN is false).

To check if a value is NaN, use the global isNaN() function or the more robust Number.isNaN() method (preferred as it doesn’t coerce non-numbers).

let result1: number = Math.sqrt(-1); // Results in NaN
let result2: number = parseInt("not a number"); // Results in NaN
let result3: number = 0 / 0; // Results in NaN
console.log(`Result 1: ${result1}`); // NaN
console.log(`result1 === NaN: ${result1 === Number.NaN}`); // false!
console.log(`isNaN(result1): ${isNaN(result1)}`); // true
console.log(`Number.isNaN(result1): ${Number.isNaN(result1)}`); // true (preferred)
console.log(`Number.isNaN("hello"): ${Number.isNaN("hello")}`); // false
console.log(`isNaN("hello"): ${isNaN("hello")}`); // true (isNaN coerces "hello" to NaN)

Number values (primitives) have access to methods from the Number.prototype.

MethodDescriptionExample
toExponential(fractionDigits?)Returns a string representing the number in exponential notation.(12345).toExponential(2) is '1.23e+4'
toFixed(digits?)Returns a string representing the number in fixed-point notation (with a specific number of decimal places).(3.14159).toFixed(2) is '3.14'
toLocaleString(locales?, options?)Returns a language-sensitive string representation of the number.(123456.789).toLocaleString('de-DE') might be '123.456,789'
toPrecision(precision?)Returns a string representing the number to a specified total number of significant digits.(123.456).toPrecision(4) is '123.5'
toString(radix?)Returns a string representation of the number in the specified base (radix). Default is base 10.(255).toString(16) is 'ff', (10).toString(2) is '1010'
valueOf()Returns the primitive value of the number object (less commonly used directly).let n = new Number(10); n.valueOf() is 10
let pi: number = 3.1415926535;
let largeNum: number = 1_234_567.89; // Underscores for readability
console.log(`pi.toFixed(3): ${pi.toFixed(3)}`); // 3.142
console.log(`largeNum.toExponential(1): ${largeNum.toExponential(1)}`); // 1.2e+6
console.log(`largeNum.toLocaleString('en-US'): ${largeNum.toLocaleString('en-US')}`); // 1,234,567.89
console.log(`(15).toString(16): ${(15).toString(16)}`); // f
console.log(`(15).toString(2): ${(15).toString(2)}`); // 1111

Always prefer using the primitive type number for annotations (let x: number). Avoid using the object type Number (let x: Number) unless you have a specific reason, as it refers to the object wrapper type, not the primitive.