Javascript BigInt Type

Introduction

Javascript BigInt represents whole numbers larger than 2^53 - 1.

2^53-1 is the largest number of JavaScript Number primitive.

Number.MAX_SAFE_INTEGER constant returns 2^53-1.

BigInt is for large integers.

A BigInt is created by appending n to the end of an integer literal.

We can create a BigInt by 1n or calling the function BigInt().

let a = 1234n/*from  w w w  .j av  a  2 s . co  m*/
console.log(a);
a = BigInt(9007199254740991);
console.log(a);
a = BigInt("9007199254740991");
console.log(a);
a = BigInt("0x1fffffffffffff");
console.log(a);
a = BigInt("0b11111111111111");
console.log(a);

BigInt cannot use Math object and cannot be mixed with instances of Number in operations.

Type information

When tested against typeof, a BigInt will give "bigint":

console.log(typeof 1n === 'bigint');
console.log(typeof BigInt('1'));

When wrapped in an Object, a BigInt is a normal "object" type:

console.log(typeof Object(1n));



PreviousNext

Related