JavaScript bigint Masterclass

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

JavaScript bigint Masterclass

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Basics

const bigInt = 1000000000000n;

console.log(typeof bigInt); // 'bigint'

console.log(-100n); // -100n

console.log(10 ** 500); // Infinity
console.log(10n ** 500n); // 1000000000000.......00n

console.log(9999999999999999); // 10000000000000000 😱
console.log(9999999999999999n); // 9999999999999999n 😎
console.log(BigInt('9999999999999999')); // 9999999999999999n 😎

const parsed = BigInt('100'); // 100n
console.log(parsed === 100n); // true

console.log(BigInt('0x64') === 100n); // true

BigInt and number interop

console.log(Number(100n) === 100); // true
console.log(BigInt(100) === 100n); // true
try {
BigInt(100.5);
} catch (e) {
console.log(e.message);
// The number 100.5 cannot be converted to a BigInt because it is not an integer
}
console.log(Number(10n ** 500n)); // Infinity
console.log(Number(9007199254740993n)); // 9007199254740992 😱
try {
console.log(10 + 20n);
} catch (e) {
console.log(e.message);
// Cannot mix BigInt and other types, use explicit conversions
}
console.log(10 + Number(20n)); // 30

Mathematical Operators

console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 😱

const completelySafe = BigInt(Number.MAX_SAFE_INTEGER);
console.log(completelySafe + 1n); // 9007199254740992n
console.log(completelySafe + 2n); // 9007199254740993n 😎

console.log(20n * 2n); // 40n
console.log(100n - 1n); // 99n
console.log(100n ** 1000n); // 1000000000000.......00000n
console.log(3n % 2n); // 1n

console.log(3n / 2n); // 1n (not 1.5)

Comparison Operators

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

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

console.log(9007199254740993n > (9007199254740991 + 2)); // true 😱
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Modern JavaScript includes a built-in primitive type called big in which SS n a suggests can be used to represent arbitrary large intes. We can create a big end by writing a number as you normally put, and adding the suffix N to specify that this is a not just a number but instead it is a big int. It is a primitive type within JavaScript and the type of operator does work on it and it gives us back the string big int as the name also implies fractions cannot be a part of intes and big ins can only be intes. However, you can have a negative or positive big ints.

00:35

And the key differentiator between a big int and a number is that a big int can be as big as you want. As an example, if you try to calculate 10 to the power of 500 with just JavaScript numbers, that value would be too big for a number and in fact it gives us that special value infinity that we looked at in our number lesson. However, no integer is too big for a big int, so 10 to the power 500 as big ins will happily calculate 1 0 0 0 as many as you need with numbers. It is actually not that hard to run out of the safe integer space. For example, this particular number as a number data type would actually translate poorly

01:13

and you'll actually see 1 0 0 0 instead of 9, 9, 9, 9. However, the same value as a big int will be perfectly fine, as you mentioned, as big as you want. JavaScript also provides a big into construc, which can be used to convert as an example, strings of arbitrary inte size into big ints. Essentially passing the string to the big end constructor is parsing, and of course equality works with begins as well. This par value is exactly equal to a hundred N and this parsing logic also works with other number systems that we looked at in our number tutorial.

01:48

So the Hector Decimal 64 does still pass to a hundred and in decimal terms you can convert between a big end and a number, and safeguards exist to prevent silly mistakes, but even then you should really consider moving between the two. As an example, we can convert a big end into a number by passing the big end to the number constructor. And as you can see, a hundred and converted to a number is equal to a hundred in JavaScript. Similarly, we have already seen that we can convert a number into a big end by passing it to the big end constructor.

02:20

So the big end version of a hundred is equal to a hundred N. However, not all numbers can be converted to a big end. For example, if we have a number with a fractional part and we try to convert it into a big end, we will get an error in JavaScript and the error message will be that it cannot be converted to a big end because it is not an inte. Similarly, if you have a very large big end that cannot be represented as a number, then we will get back in infinity. And in the worst case, if it is still within a reasonable limit, we might even get the value wrong. For anything that is beyond max safe integer,

02:53

you really shouldn't be converting a big end into a number and for the same reason of possible loss of Precision, JavaScript doesn't allow you to mix numbers and big ins in standard mathematical operators. For example, in this particular case, it's going to throw an error that you cannot mix big ins and other types. You should instead use explicit conversions. And the intent here is that if you explicitly convert using the number constructor or the big in constructor, you are sort of accepting the risk that you might lose some precision. So if you wrap the 20 N into a number constructor, we can still do math between a number and a number big in supports all the

03:28

standard mathematical operators that you are familiar with for numbers, but with a slight caveat that fractions are obviously going to get ignored. We know from our lesson on numbers that any math beyond the max safe integer is not okay to do with the built-in number data type. However, with the big in, it's completely fine to do as big a math as you want. In addition to the addition operator, feel free to use other operators as well. For example, the multiplication star, the subtraction minus the power star, star as well as the remainder person sign. However, the one operator that you need to be cautious with is of course the division

04:02

operator. Because begins don't represent fractions, all of the fractional part will actually get completely ignored in the output begins support the standard comparison operators. And if you're only comparing begins, then it's perfectly safe. But if you throw numbers into the mix, it's a different story you can compare begins with other begins using the standard strict equality. You can check if one begin is less than the other or if it is greater than the other, or even if it is greater than or equal to or less than or equal to, and they all work perfectly fine. However, you should be careful when comparing big ins and numbers.

04:37

You cannot use strict equality as the strict equality cares about the types being the same. However you can use those equality, but that doesn't mean that it is a good idea and the other comparison operators seem to work reliably as well. However, be won. Since not all intes can be represented using the number data type, you might result in values that should be equal yet they are not. The guidance here is that pick a data type that is more appropriate for your use case and then run with it. Don't mix begins and numbers unless you are really sure that the values they contain are safe.

05:11

Biggin is another one of those JavaScript built-ins that you will most likely never use directly, but having a built-in primitive integer that can be as large as you want is important for JavaScript. It allows libraries to use Biggins internally to do big competitions and even provide nice reliable wrappers for mathematical things like big decimals. As always, thank you for joining me and I will see you in the next one.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36