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.

Frequently asked questions

How does JavaScript handle large numbers?
This task is covered in 2 stacks on this page: JavaScript, TypeScript. The "BigInt" snippet in JavaScript uses `const big = 123n;`.
Which code does the JavaScript example use?
The "BigInt" snippet uses `const big = 123n;`, from the Numbers & Math section of the JavaScript cheat sheet.
Which stacks cover "large numbers" on this page?
JavaScript, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "BigInt": Cannot mix BigInt and Number in arithmetic (throws TypeError). Convert one type first. No Math methods work with BigInt.