如何在 JS 中计算 BigInt 的模

weg*_*gry 4 javascript bigint

给定 JS 中的 BigInt,如何计算它的模?

10n % 3 // Uncaught TypeError: can't convert BigInt to number
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题与类似链接的问题之间的区别在于,这个问题仅与 BigInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt相关,这是相当新的。

Alt*_*eos 8

涉及 BigInt 的算术运算的两个操作数都必须是 BigInt。在你的情况下:

// BigInt literal
10n % 3n;

// BigInt explicit conversion
10n % BigInt(3);
Run Code Online (Sandbox Code Playgroud)