Large numbers

2 snippets across 2 stacks — JavaScript, TypeScript

JSJavaScript

BigInt

JS · Numbers & Math
syntax
const big = 123n;
const big = BigInt(value);
example
const huge = 9007199254740993n;
console.log(huge + 1n);   // 9007199254740994n
console.log(huge * 2n);   // 18014398509481986n

// Convert between types
console.log(Number(100n)); // 100
console.log(BigInt(42));   // 42n

Note Cannot mix BigInt and Number in arithmetic (throws TypeError). Convert one type first. No Math methods work with BigInt.

TSTypeScript

symbol & bigint

TS · Basic Types
syntax
let varName: symbol = Symbol(description);
let varName: bigint = valueBigInt;
example
const uniqueKey: unique symbol = Symbol("cacheKey");
let regularSym: symbol = Symbol("temp");

let hugeNumber: bigint = 9007199254740993n;
let anotherBig: bigint = BigInt("123456789012345678");
output
// unique symbol creates a distinct type; bigint handles arbitrarily large integers

Note unique symbol can only be used with const declarations and creates a specific subtype of symbol. bigint cannot be mixed with number in arithmetic — you must explicitly convert. bigint requires target ES2020 or later in tsconfig.