This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
4.2.1 JS BigInt
JavaScript BigInt variables are used to store big integer values that are too big to be represented by a normal JavaScript Number.
JavaScript Integer Accuracy
JavaScript integers are only accurate up to 15 digits:
Integer Precision
let x = 999999999999999;
let y = 9999999999999999;
Try it yourself
- In JavaScript, all numbers are stored in a 64-bit floating-point format (IEEE 754 standard).
- With this standard, large integer cannot be exactly represented and will be rounded.
- Because of this, JavaScript can only safely represent integers:
- Up to 9007199254740991 +(253-1)
- and
- Down to -9007199254740991 -(253-1).
- Integer values outside this range lose precision.
How to Create a BigInt
To create a BigInt, append n to the end of an integer or call BigInt():
Examples
let x = 9999999999999999;
let y = 9999999999999999n;
Try it yourself
let x = 1234567890123456789012345n;
let y = BigInt(1234567890123456789012345)
Try it yourself
BigInt: A new 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:
- String
- Number
- Bigint
- Boolean
- Undefined
- Null
- Symbol
- Object
BigInt Operators
Operators that can be used on a JavaScript Number can also be used on a BigInt.
BigInt Multiplication Example
let x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;
Notes
- 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).
BigInt Decimals
A BigInt can not have decimals.
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
BigInt Hex, Octal and Binary
BigInt can also be written in hexadecimal, octal, or binary notation:
BigInt Hex Example
let hex = 0x20000000000003n;
let oct = 0o400000000000000003n;
let bin = 0b100000000000000000000000000000000000000000000000000011n;
Try it yourself
Precision Curiosity
Rounding can compromise program security:
MAX_SAFE_INTEGER Example
9007199254740992 === 9007199254740993; // is true !!!
Try it yourself
Minimum and Maximum Safe Integers
- ES6 added max and min properties to the Number object:
- MAX_SAFE_INTEGER
- MIN_SAFE_INTEGER
MAX_SAFE_INTEGER Example
let x = Number.MAX_SAFE_INTEGER;
try it yourself
MIN_SAFE_INTEGER Example
let x = Number.MIN_SAFE_INTEGER;
Try it yourself
New Number Methods
- ES6 also added 2 new methods to the Number object:
- Number.isInteger()
- Number.isSafeInteger()
The Number.isInteger() Method
The Number.isInteger() method returns true if the argument is an integer.
Example: isInteger()
Number.isInteger(10);
Number.isInteger(10.5);
Try it yourself
The Number.isSafeInteger() Method
- A safe integer is an integer that can be exactly represented as a double precision number.
- The Number.isSafeInteger() method returns true if the argument is a safe integer.
Example isSafeInteger()
Number.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);
Try it yourself
- Safe integers are all integers from -(253 - 1) to +(253 - 1).
- This is safe: 9007199254740991. This is not safe: 9007199254740992.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Strings part 1
JS Strings part 2
JS Strings part 3
JS Strings length Property
JS Strings the escape sequence \"
JS Strings the escape sequence \'
JS Strings the escape sequence \\
JS Statements
JS Strings line break
JS Strings line break +
JS Strings line break NOT
JS Strings typeof
JS Strings - equal value
JS Strings - not equal type
JS Strings - cannot compare Objects
JS Strings - Objects cannot be compared
JS Strings slice()
JS Strings The slice() Method part 1
JS Strings The slice() Method part 2
JS Strings The slice() Method part 3
JS Strings the substring() method
JS Strings the substr() method part 1
JS Strings the substr() method part 2
JS Strings the substr() method part 3
JS Strings the replace() method part 1
JS Strings the replace() method part 2
JS Strings the replace() method part 3
Mobile test of using JS CSS
JS Strings the replace() method part 4
JS Strings the replace() method part 5
JS Strings Convert string part 1
JS Strings Convert string part 2
JS Strings Convert string part 3
JS Strings Convert string part 4
JS Strings The padStart() Method part 1
JS Strings The padStart() Method part 2
JS Strings The padStart() Method part 3
JS Strings The padEnd() Method part 1
JS Strings The padEnd() Method part 2
JS Strings The padEnd() Method part 3
JS Strings The charAt() Method
JS Strings The charCodeAt() Method
JS Strings Property Access part 1
JS Strings Property access part 2
JS String Methods String split() part 1
JS String Methods String split() part 2
JS String The indexOf() Method
JS Numbers part 1
JS Numbers part 2
JS Numbers part 3
Floating Point Precision part 1
Floating Point Precision part 2
Numbers and Strings part 1
Numbers and Strings part 2
Numbers and Strings part 3
Numbers and Strings part 4
Numbers and Strings part 5
Numbers and Strings part 6
Numbers and Strings part 7
Numbers and Strings part 8
JS Numbers, NaN (Not a Number) part 1
JS Numbers, NaN (Not a Number) part 2
JS Numbers, NaN (Not a Number) part 3
JS Numbers, NaN (Not a Number) part 4
JS Numbers, NaN (Not a Number) part 5
JS Numbers, NaN (Not a Number) part 6
JS Numbers Infinity part 1
JS Numbers Infinity part 2
JS Numbers Infinity part 3
JS Numbers - Hexadecimal part 1
JS Numbers - Hexadecimal part 2
JS Numbers As Objects part 1
JS Numbers As Objects part 2
JS Numbers As Objects part 3
JS Numbers As Objects part 4
JS Number Methods part 1
JS Number Methods part 2
JS Number Methods part 3
JS Number Methods part 4
JS Number Methods part 5
JS Number Methods part 6
JS Number Methods part 7
JS Number Methods part 8
JS Number Methods part 9
JS Number Methods MAX_VALUE Property
JS Number Methods MIN_VALUE Property
JS Number Methods POSITIVE_INFINITY Property part 1
JS Number Methods POSITIVE_INFINITY Property part 2
JS Number Methods NEGATIVE_INFINITY Property part 1
JS Number Methods NEGATIVE_INFINITY Property part 2
JS Number Methods NaN part 1
JS Number Methods NaN part 2
JS Number Methods Number Properties
JS Arrays part 1
JS Arrays part 2
JS Arrays part 3
JS Arrays part 4
JS Arrays part 5
JS Arrays part 6
JS Arrays part 7
JS Arrays part 8
JS Arrays part 9
JS Arrays part 10
JS Arrays part 11
JS Arrays part 12
JS Arrays part 13
JS Arrays part 14
JS Arrays part 15
JS Arrays part 16
JS Arrays part 17
JS Arrays part 18
JS Arrays part 19
JS Arrays part 20
JS Arrays part 21
JS Arrays isArray() Method
JS Arrays isArray() function
JS Arrays The instanceof Operator
JS Arrays The toString() Method
JS Arrays The join() Method
JS Arrays The pop() Method part 1
JS Arrays The pop() Method part 2
JS Arrays The push() Method part 1
JS Arrays The push() Method part 2
JS Arrays The shift() Method part 1
JS Arrays The shift() Method part 2
JS Arrays The shift() Method part 3
JS Arrays The shift() Method part 4
JS Arrays Bracket Indexing
JS Arrays The length Property
JS Arrays The delete Method
JS Arrays The splice() Method part 1
JS Arrays The splice() Method part 2
JS Arrays The splice() Method part 3
JS Arrays The concat() Method part 1
JS Arrays The concat() Method part 2
JS Arrays The concat() Method part 3
JS Arrays The slice() Method part 1
JS Arrays The slice() Method part 2
JS Arrays The slice() Method part 3
JS Arrays The slice() Method part 4
JS Arrays The toString() Method part 1
JS Arrays The toString() Method part 2
JS Arrays The sort() Method part 1
JS Arrays Sort in Reverse
JS Arrays The sort() Method part 2
JS Arrays The sort() Method part 3
JS Arrays The sort() Method part 4
JS Arrays The sort() Method part 5
JS Arrays The sort() Method part 6
JS Arrays The sort() Method part 7
JS Arrays The sort() Method part 8
JS Array Sort The Highest Number
JS Array Sort The lowest number
JS Arrays sort the highest number
JS Arrays sort the lowest number
JS Arrays sort car objects by age
JS Arrays sort car objects by type
JS Arrays The forEach() Method part 1
JS Arrays The forEach() Method part 2
JS Arrays The map() Method part 1
JS Arrays The map() Method part 2
JS Arrays The filter() Method part 1
JS Arrays The filter() Method part 2
JS Arrays The reduce() Method part 1
JS Arrays The reduce() Method part 2
JS Arrays The reduce() Method part 3
JS Arrays The reduceRight() Method part 1
JS Arrays The reduceRight() Method part 2