Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Typed Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Programming JS References JS UTF-8 Characters JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript BigInt

What is JavaScript BigInt?

BigInt is a JavaScript data type for handling and storing big integer values.

BigInt allows you to work with integers larger than the limit of the Numbers.

BigInts can represent integers of any size, limited only by available memory.


JavaScript Accuracy

JavaScript Numbers are only accurate up to 15 digits:

Example

// 15 digits:
let x = 999999999999999;

// 16 digits:
let y = 9999999999999999;
Try it Yourself »

Numbers are 64-bits Floating Point

All JavaScript Numbers are stored in a 64-bit floating-point format (IEEE 754 standard).

With this standard, large numbers cannot be exactly represented, but will be rounded.

JavaScript can only safely represent integers up to 253-1 (9007199254740991).

JavaScript can only safely represent integers down to -253-1 (-9007199254740991).

Examples

// MAX = 9007199254740991
let x = Number.MAX_SAFE_INTEGER;

// MIN = -9007199254740991
let y = Number.MIN_SAFE_INTEGER;
Try it Yourself »

Integers bigger than Number.MAX_SAFE_INTEGER will lose precision:

// Max (accurate)
let x = 9007199254740991;

// Max + 10 (inaccurate)
let y = x + 10;
Try it Yourself »

Integers less than than Number.MIN_SAFE_INTEGER will lose precision:

// Min (accurate)
let x = -9007199254740991;

// Min - 10 (inaccurate)
let y = x - 10;
Try it Yourself »

Note

There is no such thing as a JavaScript Integer.

All JavaScript Numbers are 64-bit floating point.


How to Create a BigInt

You can create a BigInt in two ways:

  • Using an integer literal with an n suffix
  • Using the BigInt() constructor with a string

Examples

// Using an integer literal with an n suffix:
let x = 999999999999999n;

// Using the BigInt() constructor with a string:
let y = BigInt("999999999999999");
Try it Yourself »
let x = 12345678901234567890n;
let y = BigInt("12345678901234567890")
Try it Yourself »

You can also create a BigInt using the Bigint() constructor with a Number.

Warning !! Numbers are only accurate up to 15 digits.

Examples

let x = BigInt(9999999999999999);
Try it Yourself »

BigInt is a JavaScript Datatype

The JavaScript typeof a BigInt is "bigint":

Example

let x = BigInt(999999999999999);

let type = typeof x;
Try it Yourself »

BigInt is the second numeric data type in JavaScript (after Number).

With BigInt the total number of supported data types in JavaScript is 8:

1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object


Arithmetic Operators

BigInt supports arithmetic operators.

(+, -, ++, --, *, /, %, **)

Example

Multiplication:

let x = 9007199254740995n;
let y = 9007199254740995n;

let z = x * y;
Try it Yourself »

Note

Mixing BigInts with Numbers in arithmetic operations will result in a TypeError.

Explicit conversion must be done first.


Mixing BigInt and Numbers

Arithmetic between a BigInt and a Number is not allowed (type conversion lose information).

Unsigned right shift (>>>) can not be done on a BigInt (it does not have a fixed width).

Example

You cannot mix BigInt and Number directly:

let x = 10n;
let y = 5;

let z = x + y; // ❌ TypeError

To fix that, explicitly convert one:

let x = 10n;
let y = 5;

let z = Number(x) + y;
Try it Yourself »

BigInt / Number Conversions

BigInt to Number: Use the Number() constructor.

Number to BigInt: Use the BigInt() constructor.

Example

// Create a BigInt
let largeNumber = BigInt("12345678901234567890");

// Conversions
let num = Number(largeNumber);
let big = BigInt(num);
Try it Yourself »

Note

Large BigInts might result in Infinity or loss of precision when converted to number.

Attempting to convert a number with a fractional part to a BigInt will throw an error.


BigInt Decimals

A BigInt can not have decimals.

let x = 1.5n; // ❌ TypeError

BigInt Division Example

let x = 5n;
let y = x / 2;
// ❌ Error: Cannot mix BigInt and other types, use explicit conversion.
let x = 5n;
let y = Number(x) / 2;
Try it Yourself »

Comparison operators

Bigint supports comparison operators.

(<, > ==, ===, !==, <=, >=)

BigInts can be compared using standard comparison operators, but strict equality (===) between a BigInt and a Number will always be false due to different types.

Example

Comparisons work normally:

// true
let x = (10n > 5n);

// false (different types)
let y = (10n === 10);

// true (loose equality)
let z = (10n == 10);
Try it Yourself »

Bitwise Operators

BigInt supports bitwise operations, but only with other BigInts (not Numbers):

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (NOT)

Example

let a = 5n; // 0101
let b = 3n; // 0011

let x = (a & b); // 1n (0001)
let y = (a | b); // 7n (0111)
let z = (a ^ b); // 6n (0110)
let n = (~a);    // -6n
Try it Yourself »

BigInt Hex, Octal and Binary

BigInt can also be written in hexadecimal, octal, or binary notation:

Like numbers, bigint literals support several bases:

  • Normal: 256n
  • Octal: 0o400n
  • Hexadecimal: 0x100n
  • Binary: 0b100000000n

Examples

let num = 256n;
let oct = 0o400n;
let hex = 0x100n;
let bin = 0b100000000n;
Try it Yourself »
let hex = 0x20000000000003n;
let oct = 0o400000000000000003n;
let bin = 0b100000000000000000000000000000000000000000000000000011n;
Try it Yourself »

Precision Curiosity

Maximum safe integer in JavaScript is 9007199254740991.

Rounding can compromise program security:

MAX_SAFE_INTEGER Examples

9007199254740992 === 9007199254740993; // is true !!!
Try it Yourself »
9007199254740992n === 9007199254740993n; // is false !!!
Try it Yourself »

Summary

BigInt allows arbitrary-precision integers

BigInt numbers can be as large (or small) as your memory allows.

BigInt are used for very large integers (cryptography, IDs, timestamps, etc).

BigInt is not suitable for decimals - only integers.

Math functions (like Math.sqrt()) do not work with BigInts.

JSON.stringify() cannot handle BigInts - throws an error.


Browser Support

BigInt() is an ES2020 feature.

ES2020 is fully supported in all modern browsers since September 2020:

Chrome
85
Edge
85
Firefox
79
Safari
14
Opera
71
Aug 2020 Aug 2020 Mar 2020 Sep 2020 Sep 2020



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.