jsrosetta

Types

Big Numbers

How JavaScript's BigInt compares to Go's math/big, Java's java.math.BigInteger, and Rust's num-bigint crate — Swift has no built-in big-number type.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.60Swift ≥ 2.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

JavaScript's number is only safe up to 2^53 - 1; beyond that you need BigInt, a separate primitive with an n suffix. Go has no big-number type built into the language — instead you use big.Int from the math/big package, a struct with methods (SetUint64, SetString, Cmp…) in place of operators. Java has java.math.BigInteger built into java.base, with a design nearly identical to Go's. Rust has nothing in std, so you reach for the num-bigint crate. Swift has neither in its stdlib nor in Foundation — you need a third-party package if you need truly large numbers.

Creating and comparing big numbers

let bn = 75n
console.log(bn.toString(10))
 
bn = BigInt('75')
console.log(bn.toString(10))
 
bn = BigInt(0x4b)
console.log(bn.toString(10))
 
bn = BigInt('0x4b')
console.log(bn.toString(10))
 
bn = BigInt('0x' + Buffer.from('4b', 'hex').toString('hex'))
console.log(bn.toString(10))
console.log(Number(bn))
console.log(bn.toString(16))
console.log(Buffer.from(bn.toString(16), 'hex'))
 
let bn2 = BigInt(100)
let isEqual = bn === bn2
console.log(isEqual)
 
let isGreater = bn > bn2
console.log(isGreater)
 
let isLesser = bn < bn2
console.log(isLesser)
# Node.js
75
75
75
75
75
75
4b
<Buffer 4b>
false
false
true
 
# Go
75
75
75
75
75
75
4b
[75]
false
false
true
 
# Rust
75
75
75
75
75
75
4b
[75]
false
false
true
 
# Swift
75
75
75
75
75
75
4b
[75]
false
false
true
 
# Java
75
75
75
75
75
75
4b
[75]
false
false
true

Reference: github.com/miguelmota/golang-for-nodejs-developers#big-numbers