BigInt in JavaScript

This is a recent addition to the language.

BigInt is a special numeric type that provides support for integers of arbitrary length.

BigInt is created by appending n to the end of an integer literal or by calling the function BigInt that creates bigints from strings, numbers etc.

Math operators

BigInt can mostly be used as a regular number, for example:

Please note: the division 5/2 returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints.

We can’t mix bigints and regular numbers:

We should explicitly convert them if needed: using either BigInt() or Number(), like this:

The conversion operations are always silent, never give errors, but if the BigInt is too huge and won’t fit the number type, then extra bits will be cut off, so we should be careful doing such conversion.

The unary plus is not supported on Bigints

So we should use Number() to convert a BigInt to a number.

Comparisons

Comparisons, such as <, > work with bigints and numbers just fine:

Please note though, as numbers and bigints belong to different types, they can be equal ==, but not strictly equal ===:

Boolean operations

When inside if or other boolean operations, bigints behave like numbers.

For instance, in if, bigint 0n is falsy, other values are truthy:

Boolean operators, such as ||, && and others also work with bigints similar to numbers:

--

--

Developer with 3 yrs of industrial experience in developing scalable web applications.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.