Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

JavaScript Bigint

The bigint is a new primitive data type in JavaScript. It was introduced in ES11 version of JavaScript. it represents the whole number. It can hold numbers larger than 253 – 1. The BigInt uses the arbitrary-precision arithmetic. This article covers how to use bigInt, the arithmetic operations allowed, how to convert bigint to number and number to bigint, etc.

Defining bigint

There are two ways in which you create bigint.

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

                            
let big1 = 9007199254740991n;
console.log(typeof(big1))
 
 
//******output***
//bigint
                            
                        
                            
                        

You can even use binary, octal or hexadecimal notation. Just use n at the end. Legacy octal syntax (0640) is not allowed, only new-style (0o064n).

                            
let bigHex = 0xffffffffffffffn  
console.log(bigHex);          //72057594037927935n
 
const bigBin = 0b11111111111111111111111111111111111111111111111111111n
console.log(bigBin)          //9007199254740991n
 
const bigOct = 0o24045162451214n
console.log(bigOct)          //1379385627276n
 
                            
                        
                            
                        

or by calling the function Global function BigInt()

                            
let big2 = BigInt(9007199254099);
console.log(typeof(big2))
 
 
//******output***
//bigint                   
                        
                            
                        
                               
let bigNum= BigInt("9045141578140991");
console.log(bigNum)        //9045141578140991n  
 
 
 
                            
                        
                            
                        

Hex, binary & octal numbers.

                               
let bigHex = BigInt("0xffffffffffffff");  
console.log(bigHex);          //72057594037927935n
 
const bigBin = BigInt("0b11111111111111111111111111111111111111111111111111111");    
console.log(bigBin)          //9007199254740991n
 
const bigOct = BigInt("0o24045162451214n")
console.log(bigOct)          //1379385627276n
 
 
 
                            
                        
                            
                        

Max Value

The bigint can handle the large values and not limited as the Number datatype, which has (Number.MAX_SAFE_INTEGER). BigInt is limited by the available memory of the host system

Arithmetic Operations

The BigInt can be used with the following arithmetic operations +, *, -, **, %. Bitwise operators like &, | , ^ , ~,<< >>, (except >>> Zero fill right shift) operators.

The unary operator + is not supported. Also >>> is not supported, as all BigInts are signed

                            
let bigVar=9007199254740991n;
 
 let b=bigVar+1n;
 console.log(b);      //9007199254740992n
  
 b=bigVar*10n;
 console.log(b);     //90071992547409910n
  
 b=bigVar/5n;
 console.log(b);     //1801439850948198n
 
                            
                        
                            
                        

The / division operator rounds of the final result to the whole number. For example, dividing 5/2 results in 2 and not 2.5. i.e it is an integer and not decimal.

                            
console.log(4n / 2n);    //2n
console.log(5n / 2n);    //2n  and not 2.5 
 
 
                            
                        
                            
                        

bigint can not be mixed with operations where numbers are involved as shown below. Either you convert number to bigint or convert bigint to number.

                               
let numVar=100;
let bigVar= 100n;
 
console.log(numVar+bigVar);
 
*** Error *****
Cannot mix BigInt and other types, use explicit conversions
 
 
 
                            
                        
                            
                        

Convert Number to BigInt

You can use the Bigint function to convert the number to a bigint

                            
 
let numVar= 100;
let bigVar=BigInt(numVar)
console.log(bigVar)
console.log(typeof(bigVar))
 
//**output
//100n
//bigint
 
 
 
                            
                        
                            
                        

Convert BigInt to Number

You can convert the bigInt to number by using the Number function. Beware of the fact that you may lose precision when you coerce bigInt to number.

                            
let bigVar= 100n;
let numVar=Number(bigVar)
console.log(numVar)
console.log(typeof(numVar))
 
//**output
//100
//number
 
 
 
                            
                        
                            
                        

Comparison operators

The comparison operators work well between bigInt and a number

        
console.log(1n < 2)     //true
console.log(2n > 1)     //true
console.log(2n > 2)     //false
console.log(2n >= 2)    //true



        
    
        
    

A BigInt 0 is loosely equal (0n==0 is true) to number 0. But is not strictly equal to a Number (0n==0 is false)

        
console.log(0n === 0)   // false
console.log(0n == 0)    // true

    
        
    

Conditionals

A BigInt behaves like a Number when converted to Boolean.

        
Boolean(0n)
      // false
 
Boolean(12n)



        
    
        
    

Summary

The bigint is a primitive type. It is added so as to support the large number. bigint cannot be used in the operations involving numbers. They must be coerced to do that. Beware of the fact that you may lose precision when you coerce bigint to number and back. You can make use of comparison operators to compare bigint with a number.

Read More

  1. JavaScript Tutorial
  2. Data Types in JavaScript
  3. JavaScript String
  4. Template Strings & String Interpolation
  5. Tagged Templates
  6. String to Number
  7. EPSILON & Floating Point Precision
  8. Infinity
  9. BigInt
  10. BintInt Vs Number
  11. Boolean Data Type
  12. Undefined