Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

BigInt

BigInt(value) converts a supported value into a primitive BigInt. It accepts integer-like values, strings, booleans, and BigInts, but rejects fractional Numbers and cannot be called with new.

How it works

Call BigInt(value) without new to convert a supported value into a bigint primitive. Strings are parsed as integer-literal text, booleans become 1n or 0n, and Number inputs must be integral; fractional Numbers, invalid strings, and unsupported values throw.

Syntax

BigInt(value)

Parameters

NameTypeDescription
valuestring | number | boolean | bigintThe value to convert; it may be a string, an integer, a boolean, or another BigInt.

Returns

A BigInt value.

Key points

  • The result is a primitive bigint, not a wrapper object.
  • The conversion input can be a supported string, integral Number, boolean, or BigInt.
  • Boolean inputs map directly to the BigInt values 1n and 0n.
  • String input permits surrounding whitespace and binary, octal, or hexadecimal prefixes.
  • Successful conversion returns a BigInt value.
  • This function explicitly permits conversion from Number, subject to the integer-only rule.

Common mistakes

  • Do not use new BigInt(value); the function is not constructible and using new throws a TypeError.
  • A fractional Number such as 12.5 is rejected with RangeError.
  • Boolean conversion produces BigInt values rather than preserving a boolean.
  • String input follows integer-literal parsing, including supported binary, octal, and hexadecimal prefixes.

Example: Convert values to BigInt

const fromInteger = BigInt(9007199254740993);
const fromBinary = BigInt("0b10101");

console.log(fromInteger); // 9007199254740992n
console.log(fromBinary); // 21n

Converts an integer Number and a binary-prefixed string into BigInt primitives.

When to use it

Parse integer strings

Converting user or configuration text such as "0x123" into an exact integer representation.

Convert integral Numbers

Turning an integral Number into a BigInt when subsequent calculations require bigint values.

Convert booleans

Mapping boolean flags to numeric BigInt values for bigint-based logic.

Interview questions

QWhat does BigInt() do?

BigInt(value) returns a primitive whose type is bigint. It accepts values such as strings, integers, booleans, and existing BigInts, and it must be called without new.

Follow-up: What happens with BigInt(1.2)? It throws RangeError because Number input must be integral.

QWhich Number values can BigInt() convert?

A decimal integer Number can be converted, but a non-integral Number throws RangeError. Conversion is explicit, so this is the documented route for converting an integer Number to BigInt.

Follow-up: Why can conversion alter a large Number? Number may already have lost precision before BigInt receives it; use a string or BigInt literal when exactness matters.

QHow are strings handled?

A string is interpreted using integer-literal syntax. Leading and trailing whitespace are allowed, and binary, octal, and hexadecimal prefixes can be used; an unparseable string throws SyntaxError.

Follow-up: What does BigInt("0x123") produce? It produces 291n, because the string is parsed as hexadecimal integer-literal text.

  • BigInt
  • BigInt.asIntN()
  • BigInt.asUintN()
  • BigInt64Array
  • BigUint64Array

Source: BigInt () constructor - JavaScript | MDN. Checked 2026-10-15.