大网数据广告活动
Web 标准与前端开发

TypeError: can't convert BigInt to number

· 52CE 我爱测

The JavaScript exception "can't convert BigInt to number" occurs when an arithmetic operation involves a mix of BigInt and Number values.

The JavaScript exception "can't convert BigInt to number" occurs when an arithmetic operation involves a mix of BigInt and Number values.

Message

TypeError: Cannot convert a BigInt value to a number (V8-based)
TypeError: Cannot mix BigInt and other types, use explicit conversions (V8-based)
TypeError: BigInts have no unsigned right shift, use >> instead (V8-based)
TypeError: can't convert BigInt to number (Firefox)
TypeError: Conversion from 'BigInt' to 'number' is not allowed. (Safari)
TypeError: Invalid mix of BigInt and other type in addition/multiplication/…. (Safari)
TypeError: BigInt does not support >>> operator (Safari)

Error type

TypeError.

What went wrong?

The two sides of an arithmetic operator must both be BigInts or both not. If an operation involves a mix of BigInts and numbers, it's ambiguous whether the result should be a BigInt or number, since there may be loss of precision in both cases.

The error also happens when a BigInt is implicitly converted to a number via the number coercion process. For example, if a BigInt is passed to a built-in method that expects a number.

The error can also happen if the unsigned right shift operator (>>>) is used between two BigInts. In Firefox, the message is the same: "can't convert BigInt to number".

Examples

Mixing numbers and BigInts in operations

const sum = 1n + 1;
// TypeError: can't convert BigInt to number

Instead, explicitly coerce one side to a BigInt or number.

const sum = 1n + BigInt(1);
const sum2 = Number(1n) + 1;

Using unsigned right shift on BigInts

const a = 4n >>> 2n;
// TypeError: can't convert BigInt to number

Use normal right shift instead.

const a = 4n >> 2n;

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。