wegry
wegry

Reputation: 8147

How to compute modulo on BigInts in JS

Given a BigInt in JS, how do you compute its modulo?

10n % 3 // Uncaught TypeError: can't convert BigInt to number

edit: the difference between this question and the one linked as similar is that this question only pertains to BigInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt which is fairly recent.

Upvotes: 4

Views: 3500

Answers (1)

Altareos
Altareos

Reputation: 882

Both operands of an arithmetic operation involving a BigInt must be BigInts. In your case:

// BigInt literal
10n % 3n;

// BigInt explicit conversion
10n % BigInt(3);

Upvotes: 8

Related Questions