← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

BigInt — Arbitrary Precision Integers

JavaScript ES2020 Intermediate
debt(d9/e3/b3/t7)
d9 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'silent in production until users hit it' (d9). JSON.parse() silently corrupts large integers — the value changes without any error or warning. Two different user IDs can become identical with no runtime exception, no linter warning (detection_hints.tools is empty and no standard tool catches this), and no compiler signal. The corruption only surfaces when users experience incorrect behavior.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes a small but two-sided change: serialize IDs as strings on the PHP server side and parse them as BigInt on the client. This is a localized pattern replacement across a boundary (server serialization + client parsing), slightly more than a one-liner but contained within a single data-flow concern.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Once the string-ID convention is adopted for large integers, the burden is limited to the specific data paths that handle those IDs. Other parts of the codebase are largely unaffected. However, BigInt's incompatibility with Math functions, JSON.stringify, and mixed arithmetic means any code touching those IDs pays a small ongoing tax.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception is that 'using BigInt everywhere instead of Number is safer,' but BigInt cannot represent fractional numbers and breaks Math functions, JSON serialization, and mixed arithmetic. More critically, the opposite trap — not using BigInt for large IDs — causes silent data corruption that contradicts the intuitive expectation that JSON round-trips are lossless. This contradicts how similar numeric handling works in most other languages.

About DEBT scoring →

Also Known As

BigInt big integer JS arbitrary precision JavaScript

TL;DR

JavaScript's Number type cannot safely represent integers larger than 2⁵³−1 (Number.MAX_SAFE_INTEGER). BigInt is a separate numeric type that handles integers of arbitrary size, essential for working with 64-bit IDs from databases, cryptographic values, and precise financial calculations.

Explanation

JavaScript uses IEEE 754 double-precision floating point for all numbers — integers above 2⁵³ lose precision. The number 9007199254740993 and 9007199254740994 are represented identically. BigInt literals use an 'n' suffix: 9007199254740993n. BigInt supports all arithmetic operators (+, -, *, **, /, %) and comparison operators but cannot be mixed with regular Number in arithmetic — you must explicitly convert. BigInt cannot be serialized to JSON natively (JSON.stringify throws a TypeError). It is not available in very old browsers but has broad support since 2020. PHP's int type is 64-bit on 64-bit systems, so PHP can natively handle numbers that JavaScript's Number cannot.

Watch Out

Division with BigInt truncates toward zero and discards the remainder (9n / 2n === 4n), unlike JavaScript's Number division which returns a float; there is no built-in remainder operator that returns a BigInt, forcing developers to use modulo (%) separately if they need both quotient and remainder.

Common Misconception

Using BigInt everywhere instead of Number is safer. BigInt has no fractional part — it is integers only. It cannot represent 1.5. For most numeric work, Number is correct; BigInt is only needed for integers that exceed 2⁵³.

Why It Matters

PHP APIs often return 64-bit integer IDs (database auto-increments, snowflake IDs, Unix timestamps in microseconds). When these arrive in JavaScript via JSON, Number silently corrupts them if they exceed MAX_SAFE_INTEGER. A user ID of 9007199254740994 becomes 9007199254740992 — two different users now appear identical. BigInt prevents this.

Common Mistakes

  • JSON.parse() silently corrupting large integers — JSON has no BigInt type; numbers parse as IEEE 754 doubles. Use string IDs or a custom JSON parser.
  • JSON.stringify() throwing on BigInt — add a replacer: JSON.stringify(data, (k, v) => typeof v === 'bigint' ? v.toString() : v).
  • Mixing BigInt with Math functions — Math.max(), Math.sqrt() etc. do not accept BigInt; convert to Number first (with possible precision loss).
  • Not checking browser support — BigInt requires Chrome 67+, Firefox 68+, Safari 14+; polyfilling is complex and usually not worth it.

Code Examples

✗ Vulnerable
// ❌ Large integer loses precision in JSON
// PHP sends: {"id": 9007199254740993}
// JS receives:
const data = JSON.parse('{"id": 9007199254740993}');
console.log(data.id);           // 9007199254740992 — wrong!
console.log(data.id === 9007199254740993); // false

// ❌ Mixing BigInt and Number
const a = 100n;
const b = 50;
const c = a + b; // TypeError: Cannot mix BigInt and other types
✓ Fixed
// ✅ Server sends ID as string, client parses as BigInt
// PHP: json_encode(['id' => (string) $largeId])
const data = JSON.parse('{"id": "9007199254740993"}');
const id = BigInt(data.id);     // 9007199254740993n — correct

// BigInt arithmetic
const a = 9007199254740993n;
const b = 100n;
console.log(a + b);             // 9007199254741093n

// Convert back to string for API calls
fetch(`/api/users/${id.toString()}`);

// Comparison works across types with ==
console.log(10n == 10);  // true
console.log(10n === 10); // false (different types)

Added 23 Mar 2026
Edited 16 Jun 2026
Views 45
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 1 ping S 0 pings M 1 ping T 2 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 Scrapy 6 Perplexity 5 Google 5 Ahrefs 3 Claude 2 Meta AI 2 ChatGPT 1 Majestic 1 Bing 1
crawler 28 crawler_json 5
DEV INTEL Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
For large IDs in JSON, serialize them as strings on the server side (PHP: json_encode(['id' => (string) $id])) and parse them as BigInt on the client: BigInt(data.id). This avoids precision loss during JSON parsing.
📦 Applies To
javascript ES2020 web cli
🔗 Prerequisites


✓ schema.org compliant