-: Let's now meet one of the primitive data types,
that we never talked about before
and that is BigInt.
So big and is a special type of integers
that was introduced in year 2020
and so let's quickly take a look at it.
So we learned in the first lecture of the section
that numbers are represented internally as 64 bits.
And that means that there are exactly 64 ones or zeros
to represent any given number.
Now of these 64 bits only 53 are used
to actually store the digits themselves.
The rest are for storing the position
of the decimal point and the sign.
Now, if there are only 53 bits to store the number,
that means that there is a limit
of how big numbers can be,
and we can calculate that number.
So that's two elevated to 53
and then minus one, because the numbers starts at zero.
And so that is this gigantic number right here.
And so this is essentially the biggest number
that JavaScript can safely represent, okay.
Or actually is 53.
So this is the biggest number, alright.
And it is two,
because again we are working with base two,
which has only zeros and ones.
And this number is so important
that it's even stored into the number namespace
as MAX_SAFE_INTEGER.
So this gives us the exact same number.
Now right?
So any integer that is larger than this, is not safe
and that means it cannot be represented accurately.
So let's duplicate this line here
and for example add one to this.
And so you'll see that this is not correct, right?
It only added one number
to this one where it should have been added two.
So if we do this, then we get the exact same thing.
So we keep adding numbers here
and they are always the same.
And so that means that JavaScript can
simply not represent these numbers accurately.
And so if we do calculations with numbers
that are bigger than this,
then we might lose precision, okay.
So in some numbers it does actually work
for some reason, but that's because JavaScript
behind the scenes uses some tricks
to still represent some of the unsafe numbers.
But again, sometimes that works,
sometimes it doesn't.
And so that's why we call these unsafe numbers.
So you'll see sometimes these numbers are
or at least look correct.
And sometimes they don't, okay.
So, this can be a problem sometimes
because in some situations
we might need really, really big numbers.
Way bigger than this one here,
for example, for database IDs
or when interacting with real 60 bit numbers
and these numbers are actually used in other languages.
And so we might, for example
from some API, get a number that is larger than this.
And then we have no way
of storing that in JavaScript,
at least not until now,
because now starting from IES 2020
a new primitive was added,
which is called BigInt.
Now right? And BigInt stands for big integer.
And it can be used to store numbers as large as we want.
So no matter how big, all right.
So let's say we need this number
and I'm just using random numbers here.
So if I lock this,
then you'll see well this here
which probably does not have precision
because of course it's larger than this,
but if I use the n, then this will be a BigInt.
So let's see that.
And so this n here basically transforms a regular number,
into a BigInt number.
And you see in the console here,
it then also does look different. Okay?
So this is a really really huge number,
but now JavaScript has a way
of finally displaying this number here accurately.
All right.
We can also create BigInt by using the BigInt function.
So sometimes that's necessary
and then without the n.
And so this gives us kind of the same result,
while not really, for some reason,
but I guess it is because JavaScript will first have
to still represent this number here internally,
before it can then transform it into a BigInt.
And that's the reason why here
from a certain point on this second number is different.
So this constructor function should probably only be used
with small numbers,
for example, like this.
Now, okay.
So this is how we create BigInt numbers
but let's now see some operations with these numbers.
And well, basically it's very simple.
All the usual operators still work the same.
So let's say we have one or 10,000n plus 10,000 again.
And so in this case
of course we wouldn't even need a BigInt,
but this is just to demonstrate
that the operators work just the same with BigInt.
So we get indeed 20,000
and the same is true with other operations.
So for example, a multiplication,
let's try a really huge number
10 times this and a huge number.
And so it then gives us, this result.
Now what is not possible is
to mix BigInt with regular numbers.
So let's say we have huge,
so some random number like this.
And then we have a regular number, which is just 23.
And so if I wanted to multiply them
so huge times num then we would get this error
cannot mix BigInt and other types.
All right?
And so this is where we then would have
to convert this number here to a BigInt.
And this is where the constructor function
that i showed you here becomes necessary.
And so now it is indeed going to work.
However, there are two exceptions to this
which are the comparison operators
and the plus operator when working with strings.
So let's see that.
So we can still do a BigInt,
and then for example, a greater than a normal number.
So this still works
and we still get true as expected, okay.
However, when we do this
so 20n equal, equal, equal 20,
we will get false.
But that makes sense
because JavaScript when we use the triple operator
does not do type coercion.
And in fact,
these two values here,
they have a different primitive type.
This is a regular number, and this is a BigInt.
In fact, we can check that,
Or at least I think we can,
I never did this actually.
But yeah, indeed the type of this is a BigInt,
All right.
But however, if we do the regular equality operator,
so the lose one,
then this should still be true.
Right. Because then JavaScript does the type coercion.
And so then it will coerce this one to a regular number,
and then they're both the same.
So just like, so it would even work like this.
So this is one exception that's right out here.
So logical operators are one exception.
And the other exception is when
we do string concatenations.
So let's say huge plus
and then some string here,
is really big.
Now. And so you'll see in this case,
the number isn't actually converted to a string.
So even the BigInt number, okay.
Now, one other thing that I didn't tell you up here is
that also the math operations that we talked
about earlier are not gonna work here.
For example, we cannot take this square root like this.
Q R T of 16n.
All right.
So that doesn't work.
All right.
Finally, let's take a look at what happens with divisions
because BigInt is indeed an integer.
So what happens if we do this,
10 divided by three n
and we know that with normal numbers,
this would not be an integer, right?
So 10 divided by three is 3.33 until infinity.
So here, but with BigInt,
it will simply then return the closest BigInt. Right?
Let's try it with 11 here,
and so it simply basically cuts the decimal part off,
of course with 12, it would then be four, right.
But with anything else,
it will then cut off the decimal part, okay.
And basically, this is all that I have to tell you
in an introduction video like this one about BigInt.
So this new primitive type
adds some new capabilities
to the JavaScript language.
When you really need to work with like huge numbers
just like this one here, for example.
Now in practice, you will probably not use this very much
but it's still good to know that BigInt exists
and also how it works.