Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

RangeError: BigInt negative exponent

The JavaScript exception "BigInt negative exponent" occurs when aBigInt is raised to the power of a negative BigInt value.

Message

RangeError: Exponent must be positive (V8-based)RangeError: BigInt negative exponent (Firefox)RangeError: Negative exponent is not allowed (Safari)

Error type

What went wrong?

The exponent of anexponentiation operation must be positive. Since negative exponents would take the reciprocal of the base, the result will be between -1 and 1 in almost all cases, which gets rounded to0n. To catch mistakes, negative exponents are not allowed. Check if the exponent is non-negative before doing exponentiation.

Examples

Using a negative BigInt as exponent

js
const a = 1n;const b = -1n;const c = a ** b;// RangeError: BigInt negative exponent

Instead, check if the exponent is negative first, and either issue an error with a better message, or fallback to a different value, like0n orundefined.

js
const a = 1n;const b = -1n;const quotient = b >= 0n ? a ** b : 0n;

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp