Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 811
wegry Avatar asked Sep 13 '25 14:09

wegry


1 Answers

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);
like image 193
Altareos Avatar answered Sep 16 '25 05:09

Altareos