BigInt in JavaScript

This is a recent addition to the language.

const bigint = 1234567890123456789012345678901234567890n;

const sameBigint = BigInt("1234567890123456789012345678901234567890");

const bigintFromNumber = BigInt(10); // same as 10n

Math operators

alert(1n + 2n); // 3alert(5n / 2n); // 2
alert(1n + 2); // Error: Cannot mix BigInt and other types
let bigint = 1n;
let number = 2;
// number to bigint
alert(bigint + BigInt(number)); // 3
// bigint to number
alert(Number(bigint) + number); // 3

The unary plus is not supported on Bigints

let bigint = 1n;alert( +bigint ); // error

Comparisons

alert( 2n > 1n ); // truealert( 2n > 1 ); // true
alert( 1 == 1n ); // truealert( 1 === 1n ); // false

Boolean operations

if (0n) {
// never executes
}
alert( 1n || 2 ); // 1 (1n is considered truthy)alert( 0n || 2 ); // 2 (0n is considered falsy)

--

--

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

Abhay Jain

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