WEBVTT

1
00:01.150 --> 00:04.280
<v ->Let's now meet one of the primitive data types,</v>

2
00:04.280 --> 00:06.550
that we never talked about before

3
00:06.550 --> 00:07.827
and that is BigInt.

4
00:08.930 --> 00:12.300
So big and is a special type of integers

5
00:12.300 --> 00:15.320
that was introduced in year 2020

6
00:15.320 --> 00:17.533
and so let's quickly take a look at it.

7
00:19.090 --> 00:22.030
So we learned in the first lecture of the section

8
00:22.030 --> 00:26.900
that numbers are represented internally as 64 bits.

9
00:26.900 --> 00:31.900
And that means that there are exactly 64 ones or zeros

10
00:32.240 --> 00:34.720
to represent any given number.

11
00:34.720 --> 00:38.910
Now of these 64 bits only 53 are used

12
00:38.910 --> 00:41.730
to actually store the digits themselves.

13
00:41.730 --> 00:43.760
The rest are for storing the position

14
00:43.760 --> 00:46.850
of the decimal point and the sign.

15
00:46.850 --> 00:51.030
Now, if there are only 53 bits to store the number,

16
00:51.030 --> 00:52.830
that means that there is a limit

17
00:52.830 --> 00:55.310
of how big numbers can be,

18
00:55.310 --> 00:57.630
and we can calculate that number.

19
00:57.630 --> 01:02.330
So that's two elevated to 53

20
01:03.550 --> 01:08.463
and then minus one, because the numbers starts at zero.

21
01:09.560 --> 01:13.220
And so that is this gigantic number right here.

22
01:13.220 --> 01:16.220
And so this is essentially the biggest number

23
01:16.220 --> 01:21.220
that JavaScript can safely represent, okay.

24
01:22.110 --> 01:23.573
Or actually is 53.

25
01:25.020 --> 01:28.550
So this is the biggest number, alright.

26
01:28.550 --> 01:29.590
And it is two,

27
01:29.590 --> 01:32.680
because again we are working with base two,

28
01:32.680 --> 01:34.703
which has only zeros and ones.

29
01:35.650 --> 01:37.380
And this number is so important

30
01:37.380 --> 01:40.750
that it's even stored into the number namespace

31
01:41.650 --> 01:42.650
as MAX_SAFE_INTEGER.

32
01:46.120 --> 01:49.170
So this gives us the exact same number.

33
01:49.170 --> 01:50.320
Now right?

34
01:50.320 --> 01:54.370
So any integer that is larger than this, is not safe

35
01:54.370 --> 01:58.760
and that means it cannot be represented accurately.

36
01:58.760 --> 02:00.870
So let's duplicate this line here

37
02:00.870 --> 02:03.543
and for example add one to this.

38
02:04.827 --> 02:08.870
And so you'll see that this is not correct, right?

39
02:08.870 --> 02:10.300
It only added one number

40
02:10.300 --> 02:13.263
to this one where it should have been added two.

41
02:14.400 --> 02:17.613
So if we do this, then we get the exact same thing.

42
02:18.480 --> 02:20.410
So we keep adding numbers here

43
02:20.410 --> 02:22.820
and they are always the same.

44
02:22.820 --> 02:24.890
And so that means that JavaScript can

45
02:24.890 --> 02:28.330
simply not represent these numbers accurately.

46
02:28.330 --> 02:31.280
And so if we do calculations with numbers

47
02:31.280 --> 02:32.130
that are bigger than this,

48
02:32.130 --> 02:36.310
then we might lose precision, okay.

49
02:36.310 --> 02:39.790
So in some numbers it does actually work

50
02:39.790 --> 02:42.550
for some reason, but that's because JavaScript

51
02:42.550 --> 02:45.090
behind the scenes uses some tricks

52
02:45.090 --> 02:48.540
to still represent some of the unsafe numbers.

53
02:48.540 --> 02:51.470
But again, sometimes that works,

54
02:51.470 --> 02:53.090
sometimes it doesn't.

55
02:53.090 --> 02:55.783
And so that's why we call these unsafe numbers.

56
02:57.010 --> 03:01.060
So you'll see sometimes these numbers are

57
03:01.060 --> 03:03.170
or at least look correct.

58
03:03.170 --> 03:06.193
And sometimes they don't, okay.

59
03:07.410 --> 03:10.200
So, this can be a problem sometimes

60
03:10.200 --> 03:11.890
because in some situations

61
03:11.890 --> 03:14.960
we might need really, really big numbers.

62
03:14.960 --> 03:16.750
Way bigger than this one here,

63
03:16.750 --> 03:19.860
for example, for database IDs

64
03:19.860 --> 03:23.460
or when interacting with real 60 bit numbers

65
03:23.460 --> 03:27.050
and these numbers are actually used in other languages.

66
03:27.050 --> 03:28.470
And so we might, for example

67
03:28.470 --> 03:31.910
from some API, get a number that is larger than this.

68
03:31.910 --> 03:34.040
And then we have no way

69
03:34.040 --> 03:36.990
of storing that in JavaScript,

70
03:36.990 --> 03:39.060
at least not until now,

71
03:39.060 --> 03:42.170
because now starting from IES 2020

72
03:42.170 --> 03:44.270
a new primitive was added,

73
03:44.270 --> 03:46.520
which is called BigInt.

74
03:46.520 --> 03:50.660
Now right? And BigInt stands for big integer.

75
03:50.660 --> 03:55.090
And it can be used to store numbers as large as we want.

76
03:55.090 --> 03:58.460
So no matter how big, all right.

77
03:58.460 --> 04:00.750
So let's say we need this number

78
04:00.750 --> 04:04.130
and I'm just using random numbers here.

79
04:04.130 --> 04:05.453
So if I lock this,

80
04:06.370 --> 04:09.488
then you'll see well this here

81
04:09.488 --> 04:12.200
which probably does not have precision

82
04:12.200 --> 04:14.850
because of course it's larger than this,

83
04:14.850 --> 04:19.850
but if I use the n, then this will be a BigInt.

84
04:19.862 --> 04:21.852
So let's see that.

85
04:21.852 --> 04:26.852
And so this n here basically transforms a regular number,

86
04:27.000 --> 04:29.350
into a BigInt number.

87
04:29.350 --> 04:30.750
And you see in the console here,

88
04:30.750 --> 04:34.498
it then also does look different. Okay?

89
04:34.498 --> 04:37.890
So this is a really really huge number,

90
04:37.890 --> 04:40.040
but now JavaScript has a way

91
04:40.040 --> 04:43.880
of finally displaying this number here accurately.

92
04:43.880 --> 04:45.260
All right.

93
04:45.260 --> 04:49.253
We can also create BigInt by using the BigInt function.

94
04:52.440 --> 04:54.580
So sometimes that's necessary

95
04:54.580 --> 04:56.203
and then without the n.

96
04:57.340 --> 05:00.230
And so this gives us kind of the same result,

97
05:00.230 --> 05:02.630
while not really, for some reason,

98
05:02.630 --> 05:06.240
but I guess it is because JavaScript will first have

99
05:06.240 --> 05:09.490
to still represent this number here internally,

100
05:09.490 --> 05:12.970
before it can then transform it into a BigInt.

101
05:12.970 --> 05:14.890
And that's the reason why here

102
05:14.890 --> 05:18.173
from a certain point on this second number is different.

103
05:19.260 --> 05:23.000
So this constructor function should probably only be used

104
05:23.000 --> 05:24.603
with small numbers,

105
05:25.840 --> 05:27.273
for example, like this.

106
05:28.820 --> 05:29.653
Now, okay.

107
05:29.653 --> 05:32.440
So this is how we create BigInt numbers

108
05:33.620 --> 05:37.410
but let's now see some operations with these numbers.

109
05:37.410 --> 05:40.790
And well, basically it's very simple.

110
05:40.790 --> 05:44.280
All the usual operators still work the same.

111
05:44.280 --> 05:49.280
So let's say we have one or 10,000n plus 10,000 again.

112
05:51.820 --> 05:52.653
And so in this case

113
05:52.653 --> 05:55.990
of course we wouldn't even need a BigInt,

114
05:55.990 --> 05:57.750
but this is just to demonstrate

115
05:57.750 --> 06:01.900
that the operators work just the same with BigInt.

116
06:01.900 --> 06:04.250
So we get indeed 20,000

117
06:05.450 --> 06:08.670
and the same is true with other operations.

118
06:08.670 --> 06:10.920
So for example, a multiplication,

119
06:10.920 --> 06:12.890
let's try a really huge number

120
06:16.210 --> 06:19.660
10 times this and a huge number.

121
06:19.660 --> 06:22.353
And so it then gives us, this result.

122
06:23.250 --> 06:25.410
Now what is not possible is

123
06:25.410 --> 06:28.433
to mix BigInt with regular numbers.

124
06:29.820 --> 06:32.423
So let's say we have huge,

125
06:33.780 --> 06:36.443
so some random number like this.

126
06:38.034 --> 06:41.673
And then we have a regular number, which is just 23.

127
06:44.990 --> 06:47.720
And so if I wanted to multiply them

128
06:47.720 --> 06:52.720
so huge times num then we would get this error

129
06:53.420 --> 06:56.073
cannot mix BigInt and other types.

130
06:57.370 --> 06:58.260
All right?

131
06:58.260 --> 06:59.850
And so this is where we then would have

132
06:59.850 --> 07:03.610
to convert this number here to a BigInt.

133
07:03.610 --> 07:06.419
And this is where the constructor function

134
07:06.419 --> 07:09.413
that i showed you here becomes necessary.

135
07:13.394 --> 07:16.650
And so now it is indeed going to work.

136
07:16.650 --> 07:19.490
However, there are two exceptions to this

137
07:19.490 --> 07:21.920
which are the comparison operators

138
07:21.920 --> 07:25.760
and the plus operator when working with strings.

139
07:25.760 --> 07:27.349
So let's see that.

140
07:27.349 --> 07:30.664
So we can still do a BigInt,

141
07:30.664 --> 07:34.609
and then for example, a greater than a normal number.

142
07:34.609 --> 07:36.350
So this still works

143
07:36.350 --> 07:40.103
and we still get true as expected, okay.

144
07:41.230 --> 07:42.950
However, when we do this

145
07:42.950 --> 07:47.170
so 20n equal, equal, equal 20,

146
07:47.170 --> 07:48.960
we will get false.

147
07:48.960 --> 07:50.250
But that makes sense

148
07:50.250 --> 07:53.310
because JavaScript when we use the triple operator

149
07:53.310 --> 07:55.640
does not do type coercion.

150
07:55.640 --> 07:56.800
And in fact,

151
07:56.800 --> 07:58.140
these two values here,

152
07:58.140 --> 08:00.710
they have a different primitive type.

153
08:00.710 --> 08:04.293
This is a regular number, and this is a BigInt.

154
08:05.350 --> 08:06.700
In fact, we can check that,

155
08:09.160 --> 08:11.760
Or at least I think we can,

156
08:11.760 --> 08:13.173
I never did this actually.

157
08:14.740 --> 08:18.547
But yeah, indeed the type of this is a BigInt,

158
08:19.640 --> 08:21.000
All right.

159
08:21.000 --> 08:25.930
But however, if we do the regular equality operator,

160
08:25.930 --> 08:27.063
so the lose one,

161
08:28.610 --> 08:30.313
then this should still be true.

162
08:31.410 --> 08:35.390
Right. Because then JavaScript does the type coercion.

163
08:35.390 --> 08:38.750
And so then it will coerce this one to a regular number,

164
08:38.750 --> 08:41.050
and then they're both the same.

165
08:41.050 --> 08:44.903
So just like, so it would even work like this.

166
08:46.360 --> 08:49.923
So this is one exception that's right out here.

167
08:53.010 --> 08:55.350
So logical operators are one exception.

168
08:55.350 --> 08:57.780
And the other exception is when

169
08:57.780 --> 09:01.370
we do string concatenations.

170
09:01.370 --> 09:05.240
So let's say huge plus

171
09:06.440 --> 09:08.180
and then some string here,

172
09:08.180 --> 09:12.393
is really big.

173
09:13.810 --> 09:16.300
Now. And so you'll see in this case,

174
09:16.300 --> 09:19.560
the number is actually converted to a string.

175
09:19.560 --> 09:23.190
So even the BigInt number, okay.

176
09:23.190 --> 09:27.080
Now, one other thing that I didn't tell you up here is

177
09:27.080 --> 09:30.050
that also the math operations that we talked

178
09:30.050 --> 09:32.493
about earlier are not gonna work here.

179
09:33.750 --> 09:37.683
For example, we cannot take this square root like this.

180
09:38.740 --> 09:41.723
Q R T of 16n.

181
09:44.100 --> 09:45.200
All right.

182
09:45.200 --> 09:46.593
So that doesn't work.

183
09:50.000 --> 09:51.320
All right.

184
09:51.320 --> 09:55.540
Finally, let's take a look at what happens with divisions

185
09:57.330 --> 10:00.763
because BigInt is indeed an integer.

186
10:01.860 --> 10:03.863
So what happens if we do this,

187
10:05.120 --> 10:08.820
10 divided by three n

188
10:08.820 --> 10:11.470
and we know that with normal numbers,

189
10:11.470 --> 10:14.490
this would not be an integer, right?

190
10:14.490 --> 10:19.490
So 10 divided by three is 3.33 until infinity.

191
10:19.850 --> 10:22.870
So here, but with BigInt,

192
10:22.870 --> 10:27.670
it will simply then return the closest BigInt. Right?

193
10:27.670 --> 10:29.713
Let's try it with 11 here,

194
10:31.330 --> 10:35.430
and so it simply basically cuts the decimal part off,

195
10:35.430 --> 10:39.950
of course with 12, it would then be four, right.

196
10:39.950 --> 10:41.370
But with anything else,

197
10:41.370 --> 10:45.450
it will then cut off the decimal part, okay.

198
10:45.450 --> 10:49.020
And basically, this is all that I have to tell you

199
10:49.020 --> 10:52.247
in an introduction video like this one about BigInt.

200
10:53.110 --> 10:55.040
So this new primitive type

201
10:55.040 --> 10:57.410
adds some new capabilities

202
10:57.410 --> 10:59.180
to the JavaScript language.

203
10:59.180 --> 11:02.837
When you really need to work with like huge numbers

204
11:02.837 --> 11:06.170
just like this one here, for example.

205
11:06.170 --> 11:10.920
Now in practice, you will probably not use this very much

206
11:10.920 --> 11:14.050
but it's still good to know that BigInt exists

207
11:14.050 --> 11:15.823
and also how it works.