BigInt in JavaScript

Welcome to the world of BigInt in JavaScript! This chapter delves into everything you need to know about this powerful data type, designed specifically for handling numbers that are too large to be represented accurately by standard JavaScript numbers.

Why BigInt?

  • Limitations of Numbers: Traditional JavaScript numbers (of the Number data type) are stored using 64-bit floating-point representation. This limits their ability to represent integers precisely beyond a certain range (approximately 9 quintillion or 9,007,199,254,740,991).
  • Enter BigInt: The BigInt data type was introduced in ECMAScript 2020 (ES2020) to address this limitation. It allows you to work with integers of arbitrary precision, essentially unlimited in size.

Creating BigInts

  • Literal Notation: The most straightforward way to create a BigInt is by appending the letter “n” to the end of an integer literal:
				
					const bigInt1 = 12345678901234567890n;
console.log(bigInt1); // Output: 12345678901234567890n

				
			

Using the BigInt() Function: You can also convert strings or numbers to BigInts using the BigInt() function:

				
					const bigInt2 = BigInt("9876543210123456789"); // BigInt from string
const bigInt3 = BigInt(Number.MAX_SAFE_INTEGER); // Convert from Number (careful with precision loss)
console.log(bigInt2); // Output: 9876543210123456789n
console.log(bigInt3); // Output: 9007199254740991n (may differ slightly due to rounding)

				
			

Performing Arithmetic with BigInts

Supported Operators: BigInts support arithmetic operations like addition (+), subtraction (-), multiplication (*), and exponentiation (**). However, division (/) results in a BigInt (rounded if necessary).

				
					const bigInt1 = 100n;
const bigInt2 = 50n;

const sum = bigInt1 + bigInt2;
const difference = bigInt1 - bigInt2;
const product = bigInt1 * bigInt2;
const power = bigInt1 ** 2n;
const division = bigInt1 / bigInt2; // Rounds down (always BigInt)

console.log(sum); // Output: 150n
console.log(difference); // Output: 50n
console.log(product); // Output: 5000n
console.log(power); // Output: 10000n
console.log(division); // Output: 2n

				
			

Important Considerations

Mixing BigInts and Numbers: Mixing BigInts and regular numbers in arithmetic operations can lead to unexpected results due to automatic type conversion. It’s recommended to explicitly convert numbers to BigInts for precise calculations.

				
					const bigInt = 100n;
const number = 50;

const result1 = bigInt + number; // result1 will be 150 (number converted to BigInt)
const result2 = number + bigInt; // result2 will be 150 (number converted to BigInt)

console.log(result1); // Output: 150n
console.log(result2); // Output: 150n

				
			
  • Comparison Operators: Comparison operators (=====<>, etc.) work with BigInts as expected. However, comparing BigInts with numbers might involve implicit conversion, so be cautious.

Advanced BigInt Operations

  • toString(): Converts a BigInt to a string.
  • valueOf(): Returns the primitive value of the BigInt (useful for some external libraries).
  • Bitwise Operators: BigInts support bitwise operators like & (AND), | (OR), and ^ (XOR) for bit-level manipulation (works with BigInt of the same size).
				
					const bigInt1 = 12345678901234567890n;
console.log(bigInt1); // Output: 12345678901234567890n

				
			

BigInt is a valuable addition to JavaScript's arsenal, enabling developers to work with extremely large numbers that would otherwise lose precision using the standard Number data type. While BigInts come with some limitations regarding mixing with numbers and potential conversion issues, their benefits outweigh these considerations for specific use cases. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India