Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypeError: can't convert x to BigInt

The JavaScript exception "x can't be converted to BigInt" occurs when attempting to convert aSymbol,null, orundefined value to aBigInt, or if an operation expecting a BigInt parameter receives a number.

Message

TypeError: Cannot convert null to a BigInt (V8-based)TypeError: can't convert null to BigInt (Firefox)TypeError: Invalid argument type in ToBigInt operation (Safari)

Error type

What went wrong?

When using theBigInt() function to convert a value to a BigInt, the value would first be converted to a primitive. Then, if it's not one of BigInt, string, number, and boolean, the error is thrown.

Some operations, likeBigInt.asIntN, require the parameter to be a BigInt. Passing in a number in this case will also throw this error.

Examples

Using BigInt() on invalid values

js
const a = BigInt(null);// TypeError: can't convert null to BigIntconst b = BigInt(undefined);// TypeError: can't convert undefined to BigIntconst c = BigInt(Symbol("1"));// TypeError: can't convert Symbol("1") to BigInt
js
const a = BigInt(1);const b = BigInt(true);const c = BigInt("1");const d = BigInt(Symbol("1").description);

Note:Simply coercing the value to a string or number usingString() orNumber() before passing it toBigInt() is usually not sufficient to avoid all errors. If the string is not a valid integer number string, aSyntaxError is thrown; if the number is not an integer (most notably,NaN), aRangeError is thrown. If the range of input is unknown, properly validate it before usingBigInt().

Passing a number to a function expecting a BigInt

js
const a = BigInt.asIntN(4, 8);// TypeError: can't convert 8 to BigIntconst b = new BigInt64Array(3).fill(3);// TypeError: can't convert 3 to BigInt
js
const a = BigInt.asIntN(4, 8n);const b = new BigInt64Array(3).fill(3n);

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp