Chrome 将于 5 月 20-21 日重返 Google I/O!
立即注册
BigInt
bookmark_borderbookmark
使用集合保持井井有条 根据您的偏好保存内容并对其进行分类。
BigInt 原始值是 JavaScript 中相对较新的补充,允许对超出 Number
允许范围的数字执行数学运算。要创建 BigInt,请在数字字面量的末尾附加 n
,或将整数或数字字符串值传递给 BigInt()
函数。
const myNumber = 9999999999999999;
const myBigInt = 9999999999999999n;
typeof myNumber;
> "number"
typeof myBigInt;
> "bigint"
myNumber;
> 10000000000000000
myBigInt;
> 9999999999999999n
在此示例中,9999999999999999
超出了 Number
可以安全表示的数字范围,从而导致舍入错误。
BigInt 值不继承 Number
对象提供的方法和属性,并且不能与 JavaScript 内置 Math
对象提供的方法一起使用。最重要的是,您不能在标准算术运算中混合使用 BigInt 和 Number 原始值
9999999999999999n + 5
> Uncaught TypeError: can't convert BigInt to number
要使用 BigInt 进行算术运算,您必须将两个操作数都定义为 BigInt 值
console.log( 9999999999999999 + 10 ); // Off by one
> 10000000000000010
console.log( 9999999999999999n + 10n );
> 10000000000000009n
除非另有说明,否则此页面的内容均根据 Creative Commons Attribution 4.0 License 获得许可,代码示例根据 Apache 2.0 License 获得许可。有关详细信息,请参阅 Google Developers Site Policies。Java 是 Oracle 和/或其关联公司的注册商标。
上次更新时间:2024-03-31 UTC。
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-03-31 UTC."],[],[]]