RangeError: BigInt 除以零
当BigInt
除以0n
时,会发生JavaScript异常“BigInt除以零”。
消息
RangeError: Division by zero (V8-based) RangeError: BigInt division by zero (Firefox) RangeError: 0 is an invalid divisor value. (Safari)
错误类型
哪里出错了?
示例
除以0n
js
const a = 1n;
const b = 0n;
const quotient = a / b;
// RangeError: BigInt division by zero
相反,首先检查除数是否为0n
,然后要么发出带有更好消息的错误,要么回退到其他值,例如Infinity
或undefined
。
js
const a = 1n;
const b = 0n;
const quotient = b === 0n ? undefined : a / b;