Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The number type in TypeScript is a primitive data type that represents both integer and floating-point values. Inheriting JavaScript’s numeric implementation, TypeScript evaluates all number values as IEEE 754 double-precision 64-bit floating-point formats. There is no distinct type for integers (e.g., int or float) within the standard number type.

Literal Representations

TypeScript supports strict typing for base numeral systems and scientific notation using specific literal formats:
// Decimal (Base 10)
const decimal: number = 42;
const float: number = 3.14159;

// Scientific / Exponential Notation
const sci: number = 1e10;
const sciNegative: number = 2.5e-3;

// Hexadecimal (Base 16) - Prefix: 0x or 0X
const hex: number = 0x2A;

// Binary (Base 2) - Prefix: 0b or 0B
const binary: number = 0b101010;

// Octal (Base 8) - Prefix: 0o or 0O
const octal: number = 0o52;

Numeric Separators

To improve the readability of large numeric literals, TypeScript supports the ECMAScript numeric separator feature. Underscores (_) can be used to visually group digits, but they must be placed strictly between two digits. They cannot appear at the beginning or end of a literal, adjacent to a decimal point, adjacent to a base prefix, or consecutively. These separators are stripped out during compilation and do not affect the underlying value.
// Valid separators
const billions: number = 1_000_000_000;
const bytes: number = 0xFF_00_00_FF;
const fractional: number = 0.000_001;

// Invalid separators (will cause compilation errors)
// const invalidStart = _100;      // Cannot start with a separator
// const invalidDecimal = 1_.5;    // Cannot be adjacent to a decimal point
// const invalidPrefix = 0x_A;     // Cannot be adjacent to a base prefix
// const invalidConsecutive = 1__0; // Cannot be consecutive

Special Numeric Values

The number type encompasses several special global properties that result from specific mathematical operations or represent boundary limits:
const notANumber: number = NaN;               // Result of undefined math operations (e.g., 0/0)
const positiveInfinity: number = Infinity;    // Exceeds upper bound of floating-point representation
const negativeInfinity: number = -Infinity;   // Exceeds lower bound of floating-point representation

Precision and Bounds

Because number relies on a 64-bit floating-point format, it can only safely represent integers between -(2^53 - 1) and 2^53 - 1.
const maxSafe: number = Number.MAX_SAFE_INTEGER; // 9007199254740991
const minSafe: number = Number.MIN_SAFE_INTEGER; // -9007199254740991
Integers outside this boundary will experience precision loss. For arbitrary-precision integers, TypeScript provides a distinct bigint primitive, which is not strictly compatible with number without explicit coercion.

Primitive vs. Wrapper Object

TypeScript distinguishes between the lowercase number primitive and the uppercase Number global wrapper object. Type annotations should always use the lowercase number to avoid assigning an object reference where a primitive value is expected.
// Correct: Primitive type annotation
let primitiveNum: number = 10;

// Incorrect: Object wrapper type annotation (Avoid)
let objectNum: Number = new Number(10); 
Master TypeScript with Deep Grasping Methodology!Learn More