bigint and symbol

bigint

number is exact only up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). For bigger whole numbers use bigint: add n after the number. Needs target ES2020 or later.

Example

const safe: number = Number.MAX_SAFE_INTEGER;
console.log(safe + 2); // wrong! precision lost

const big: bigint = 9007199254740991n;
console.log(big + 2n); // correct

const paise: bigint = 123456789012345678n * 100n;
console.log(paise.toString());

Output

9007199254740992
9007199254740993n
12345678901234567800

Mixing bigint and number

Example

You cannot mix bigint and number in arithmetic. TypeScript reports TS2365. Convert with BigInt(x) or Number(x).

const a: bigint = 10n;
const b: number = 5;
console.log(a + b);

Compiler output (tsc)

Line 3: error TS2365: Operator '+' cannot be applied to types 'bigint' and 'number'.

symbol

A symbol is a unique value, often used as a hidden or collision-free object key. Two symbols are never equal, even with the same description.

Example

const id1: symbol = Symbol("id");
const id2: symbol = Symbol("id");
console.log(id1 === id2);
console.log(id1.description);

const SECRET = Symbol("secret");
const user = { name: "Meera", [SECRET]: "token-123" };
console.log(Object.keys(user));
console.log(user[SECRET]);

Output

false
id
[ 'name' ]
token-123