All language subtitles for 6. Working with BigInt

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian Download
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1 1

Let's now meet one of the primitive data types, 2

2

that we never talked about before 3

3

and that is BigInt. 4

4

So big and is a special type of integers 5

5

that was introduced in year 2020 6

6

and so let's quickly take a look at it. 7

7

So we learned in the first lecture of the section 8

8

that numbers are represented internally as 64 bits. 9

9

And that means that there are exactly 64 ones or zeros 10

10

to represent any given number. 11

11

Now of these 64 bits only 53 are used 12

12

to actually store the digits themselves. 13

13

The rest are for storing the position 14

14

of the decimal point and the sign. 15

15

Now, if there are only 53 bits to store the number, 16

16

that means that there is a limit 17

17

of how big numbers can be, 18

18

and we can calculate that number. 19

19

So that's two elevated to 53 20

20

and then minus one, because the numbers starts at zero. 21

21

And so that is this gigantic number right here. 22

22

And so this is essentially the biggest number 23

23

that JavaScript can safely represent, okay. 24

24

Or actually is 53. 25

25

So this is the biggest number, alright. 26

26

And it is two, 27

27

because again we are working with base two, 28

28

which has only zeros and ones. 29

29

And this number is so important 30

30

that it's even stored into the number namespace 31

31

as MAX_SAFE_INTEGER. 32

32

So this gives us the exact same number. 33

33

Now right? 34

34

So any integer that is larger than this, is not safe 35

35

and that means it cannot be represented accurately. 36

36

So let's duplicate this line here 37

37

and for example add one to this. 38

38

And so you'll see that this is not correct, right? 39

39

It only added one number 40

40

to this one where it should have been added two. 41

41

So if we do this, then we get the exact same thing. 42

42

So we keep adding numbers here 43

43

and they are always the same. 44

44

And so that means that JavaScript can 45

45

simply not represent these numbers accurately. 46

46

And so if we do calculations with numbers 47

47

that are bigger than this, 48

48

then we might lose precision, okay. 49

49

So in some numbers it does actually work 50

50

for some reason, but that's because JavaScript 51

51

behind the scenes uses some tricks 52

52

to still represent some of the unsafe numbers. 53

53

But again, sometimes that works, 54

54

sometimes it doesn't. 55

55

And so that's why we call these unsafe numbers. 56

56

So you'll see sometimes these numbers are 57

57

or at least look correct. 58

58

And sometimes they don't, okay. 59

59

So, this can be a problem sometimes 60

60

because in some situations 61

61

we might need really, really big numbers. 62

62

Way bigger than this one here, 63

63

for example, for database IDs 64

64

or when interacting with real 60 bit numbers 65

65

and these numbers are actually used in other languages. 66

66

And so we might, for example 67

67

from some API, get a number that is larger than this. 68

68

And then we have no way 69

69

of storing that in JavaScript, 70

70

at least not until now, 71

71

because now starting from IES 2020 72

72

a new primitive was added, 73

73

which is called BigInt. 74

74

Now right? And BigInt stands for big integer. 75

75

And it can be used to store numbers as large as we want. 76

76

So no matter how big, all right. 77

77

So let's say we need this number 78

78

and I'm just using random numbers here. 79

79

So if I lock this, 80

80

then you'll see well this here 81

81

which probably does not have precision 82

82

because of course it's larger than this, 83

83

but if I use the n, then this will be a BigInt. 84

84

So let's see that. 85

85

And so this n here basically transforms a regular number, 86

86

into a BigInt number. 87

87

And you see in the console here, 88

88

it then also does look different. Okay? 89

89

So this is a really really huge number, 90

90

but now JavaScript has a way 91

91

of finally displaying this number here accurately. 92

92

All right. 93

93

We can also create BigInt by using the BigInt function. 94

94

So sometimes that's necessary 95

95

and then without the n. 96

96

And so this gives us kind of the same result, 97

97

while not really, for some reason, 98

98

but I guess it is because JavaScript will first have 99

99

to still represent this number here internally, 100

100

before it can then transform it into a BigInt. 101

101

And that's the reason why here 102

102

from a certain point on this second number is different. 103

103

So this constructor function should probably only be used 104

104

with small numbers, 105

105

for example, like this. 106

106

Now, okay. 107

107

So this is how we create BigInt numbers 108

108

but let's now see some operations with these numbers. 109

109

And well, basically it's very simple. 110

110

All the usual operators still work the same. 111

111

So let's say we have one or 10,000n plus 10,000 again. 112

112

And so in this case 113

113

of course we wouldn't even need a BigInt, 114

114

but this is just to demonstrate 115

115

that the operators work just the same with BigInt. 116

116

So we get indeed 20,000 117

117

and the same is true with other operations. 118

118

So for example, a multiplication, 119

119

let's try a really huge number 120

120

10 times this and a huge number. 121

121

And so it then gives us, this result. 122

122

Now what is not possible is 123

123

to mix BigInt with regular numbers. 124

124

So let's say we have huge, 125

125

so some random number like this. 126

126

And then we have a regular number, which is just 23. 127

127

And so if I wanted to multiply them 128

128

so huge times num then we would get this error 129

129

cannot mix BigInt and other types. 130

130

All right? 131

131

And so this is where we then would have 132

132

to convert this number here to a BigInt. 133

133

And this is where the constructor function 134

134

that i showed you here becomes necessary. 135

135

And so now it is indeed going to work. 136

136

However, there are two exceptions to this 137

137

which are the comparison operators 138

138

and the plus operator when working with strings. 139

139

So let's see that. 140

140

So we can still do a BigInt, 141

141

and then for example, a greater than a normal number. 142

142

So this still works 143

143

and we still get true as expected, okay. 144

144

However, when we do this 145

145

so 20n equal, equal, equal 20, 146

146

we will get false. 147

147

But that makes sense 148

148

because JavaScript when we use the triple operator 149

149

does not do type coercion. 150

150

And in fact, 151

151

these two values here, 152

152

they have a different primitive type. 153

153

This is a regular number, and this is a BigInt. 154

154

In fact, we can check that, 155

155

Or at least I think we can, 156

156

I never did this actually. 157

157

But yeah, indeed the type of this is a BigInt, 158

158

All right. 159

159

But however, if we do the regular equality operator, 160

160

so the lose one, 161

161

then this should still be true. 162

162

Right. Because then JavaScript does the type coercion. 163

163

And so then it will coerce this one to a regular number, 164

164

and then they're both the same. 165

165

So just like, so it would even work like this. 166

166

So this is one exception that's right out here. 167

167

So logical operators are one exception. 168

168

And the other exception is when 169

169

we do string concatenations. 170

170

So let's say huge plus 171

171

and then some string here, 172

172

is really big. 173

173

Now. And so you'll see in this case, 174

174

the number isn't actually converted to a string. 175

175

So even the BigInt number, okay. 176

176

Now, one other thing that I didn't tell you up here is 177

177

that also the math operations that we talked 178

178

about earlier are not gonna work here. 179

179

For example, we cannot take this square root like this. 180

180

Q R T of 16n. 181

181

All right. 182

182

So that doesn't work. 183

183

All right. 184

184

Finally, let's take a look at what happens with divisions 185

185

because BigInt is indeed an integer. 186

186

So what happens if we do this, 187

187

10 divided by three n 188

188

and we know that with normal numbers, 189

189

this would not be an integer, right? 190

190

So 10 divided by three is 3.33 until infinity. 191

191

So here, but with BigInt, 192

192

it will simply then return the closest BigInt. Right? 193

193

Let's try it with 11 here, 194

194

and so it simply basically cuts the decimal part off, 195

195

of course with 12, it would then be four, right. 196

196

But with anything else, 197

197

it will then cut off the decimal part, okay. 198

198

And basically, this is all that I have to tell you 199

199

in an introduction video like this one about BigInt. 200

200

So this new primitive type 201

201

adds some new capabilities 202

202

to the JavaScript language. 203

203

When you really need to work with like huge numbers 204

204

just like this one here, for example. 205

205

Now in practice, you will probably not use this very much 206

206

but it's still good to know that BigInt exists 207

207

and also how it works.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.