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

BigInt Vs NumberJava Script

In this article, we will show you the difference between BigInt and BigInt number data types in JavaScript. And also when to use the BigInt data type

BigInt Vs Number

BigInt is an integer, number is a decimal

You cannot store 100.20 in a BigInt, because it is an integer and not decimal.

         
//bigInt
var BigNum1=100n;           //ok
var BigNum2=100.20n;        /error
 
//number
var Num1=100;               //ok
var Num2=100.20;            /ok
    
        
    

They are implemented differently

bigInt is stored as arbitrary-precision integers and the number as double-precision 64-bit number.

BigInt can handle large numbers

The number can handle numbers up to 9007199254740991 ( Number.MAX_SAFE_INTEGER). It is a limitation imposed due to the double precision 64 bit number

The BigInt can handle numbers larger than that. BigInt is limited by the available memory of the host system.

BigInt is more precise

Since they are integers, they do not suffer from Floating-point precision problems. Hence operations involving them are more precise.

BigInt cannot be mixed with numbers

bigint can not be mixed with operations where numbers are involved as shown below. The JavaScript will throw Cannot mix BigInt and other types, use explicit conversions error.

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

Built in Math object does not support BigInt

We cannot use a BigInt value with methods in the built-in Math object as it only supports number types.

Coercing BigInt to number may lose precision

You can convert bigInt to a number using the Number function. But if the bigInt number is larger than Number.MAX_SAFE_INTEGER then you will loose precision

         
let numVar=100;
let bigVar= 100n;
 
console.log(numVar+ Number(bigVar));   //200
    
        
    

Convert the number to bigInt, but if the number is a decimal number, then you will loose the precision again as bigInt is integer and not decimal.

You can compare bigInt with a number

Comparing a bigInt with a number works correctly.

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


BigInt is not strictly equal to number

Comparing a bigInt with a number works correctly.BigInt is loosely equal to number, But if you use strict equality checker (===) then they are not equal as they differ in their data type

 
let numVar=100;
let bigVar= 100n;
 
console.log(numVar==bigVar)  //true
console.log(numVar===bigVar) //false


Operations on BigInt is slow

The arithmetic operations involving BigInt are slower compared to the Floating Point arithmetic of a primitive number.

The floating-point arithmetic is implemented in hardware, Hence it is faster. The Arbitrary-precision arithmetic is entirely implemented in software hence slower.