W3cubDocs

/JavaScript

BigInt() constructor

The BigInt() function returns a value of type bigint.

Syntax

BigInt(value)

Note: BigInt() can only be called without new. Attempting to construct it with new throws a TypeError.

Parameters

value

The numeric value of the object being created. It may be a string, an integer, a boolean, or another BigInt.

Return value

A BigInt value. Number values must be integers and are converted to BigInts. The boolean value true becomes 1n, and false becomes 0n. Strings are parsed as if they are source text for integer literals, which means they can have leading and trailing whitespaces and can be prefixed with 0b, 0o, or 0x.

Exceptions

RangeError

Thrown if the parameter is a non-integral number.

TypeError

Thrown if at least one of these conditions is met:

  • The parameter cannot be converted to a primitive.
  • After conversion to a primitive, the result is undefined, null, symbol.
SyntaxError

Thrown if the parameter is a string that cannot be parsed as a BigInt.

Examples

Creating a new BigInt

BigInt(123);
// 123n

Using string values

BigInt("123"); // 123n
BigInt("0b10101"); // 4161n, which is 10101 in binary
BigInt("0o123"); // 83n, which is 123 in octal
BigInt("0x123"); // 291n, which is 123 in hexadecimal
BigInt("  123  "); // 123n, leading and trailing whitespaces are allowed

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
BigInt
67
79
68
No
54
14
67
67
68
48
14
9.0
1.0
10.4.0

See also

© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt