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 SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Introduction JS Where To JS Output

JS Syntax

JS Syntax JS Statements JS Comments JS Variables JS Variables JS Let JS Const JS Types

JS Operators

JS Operators JS Arithmetic JS Assignment JS Comparisons JS Conditional JS If JS If Else JS Ternary JS Switch JS Booleans JS Logical

JS Loops

JS Loops JS Loop for JS Loop while JS Break JS Continue JS Control Flow

JS Strings

JS Strings JS String Templates JS String Methods JS String Search JS String Reference

JS Numbers

JS Numbers JS Numbers JS Number Methods JS Number Properties JS Number Reference JS Bitwise JS BigInt

JS Functions

Function Path Function Intro Function Invocation Function Parameters Function Returns Function Arguments Function Expressions Function Arrow Function Quiz

JS Objects

Object Path Object Intro Object Properties Object Methods Object this Object Display Object Constructors

JS Scope

JS Scope JS Code Blocks JS Hoisting JS Strict Mode

JS Dates

JS Dates JS Date Formats JS Date Get JS Date Set JS Date Methods

JS Arrays

JS Arrays JS Array Methods JS Array Search JS Array Sort JS Array Iterations JS Array Reference JS Array Const

JS Sets

JS Sets JS Set Methods JS Set Logic JS Set WeakSet JS Set Reference

JS Maps

JS Maps JS Map Methods JS Map WeakMap JS Map Reference

JS Iterations

JS Loops JS Iterables JS Iterators JS Generators

JS Math

JS Math JS Math Reference JS Math Random

JS RexExp

JS RegExp Flags JS RegExp Classes JS RegExp Metachars JS RegExp Assertions JS RegExp Quantifiers JS RegExp Patterns JS RegExp Objects JS RegExp Methods

JS Data Types

JS Destructuring JS Data Types JS Primitive Data JS Object Types JS typeof JS toString() JS Type Conversion

JS Errors

JS Errors Intro JS Errors Silent JS Error Statements JS Error Object

JS Debugging

JS Debugging Debugging Debug Console Debug Breakpoints Debug Errors Debug Async

JS Conventions

JS Style Guide JS Best Practices JS Mistakes JS Performance

JS References

JS Keywords Reference JS Keywords Reserved JS Operator Reference JS Operator Precedence

JS Versions

JS 2026 JS 2025 JS 2024 JS 2023 JS 2022 JS 2021 JS 2020 JS 2019 JS 2018 JS 2017 JS 2016 JS Versions JS 2015 (ES6) JS 2009 (ES5) JS 1999 (ES3) JS IE / Edge JS History

JS HTML

JS HTML DOM JS Events JS Projects New

JS Advanced

JS Temporal  New JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs 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 Numbers.

BigInt can represent an integer 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 standard JavaScript arithmetic operators.

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

Example

Multiplication:

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

let z = x * y;
Try it Yourself »

Mixing BigInt and Numbers

Arithmetic between a BigInt and a Number is not allowed (will result in a TypeError).

Explicit conversion must be done first.

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);
Try it Yourself »

Note

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


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 »

Note

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


Comparison Operators

Bigint supports standard JavaScript comparison operators.

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

BigInts can also be compared with Numbers using standard comparison operators.

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 »

Note

Strict equality (===) between a BigInt and a Number will always be false due to different types.


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 »

Bitwise Shift Operators

BigInt only supports two shift operators:

  • << (left shift)
  • >> (signed right shift)

Example

let big = 10n; // binary: 1010

let x = (big << 2n); // 40n (101000)
let y = (big >> 1n); // 5n (0101)
Try it Yourself »

Impotant Rules:

  • Both operands must be BigInt
  • Shift amounts must be non-negative
  • Right shift keeps the sign bit for negative values

Note

Unsigned right shift (>>>) is not allowed with BigInts.


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, cookies and privacy policy.

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

-->