All language subtitles for 020 Type Conversion and Coercion.en_US

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
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
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
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,372 --> 00:00:05,260 In this video, we need to go back to value types. 2 00:00:05,260 --> 00:00:09,580 So types are one of the fundamental aspects in programming 3 00:00:09,580 --> 00:00:11,720 and converting between types, 4 00:00:11,720 --> 00:00:15,010 is something that we do in every programming language. 5 00:00:15,010 --> 00:00:17,820 For example, converting a string to a number 6 00:00:17,820 --> 00:00:19,680 or a number into a Boolean 7 00:00:19,680 --> 00:00:22,110 is something that we do all the time. 8 00:00:22,110 --> 00:00:24,610 And so it's important that we learned about this 9 00:00:24,610 --> 00:00:28,170 before being able to move on further in the course. 10 00:00:28,170 --> 00:00:31,554 That's specially true for a language like JavaScript, 11 00:00:31,554 --> 00:00:34,790 which sometimes behaves in a weird way 12 00:00:34,790 --> 00:00:36,713 as we will see in this video. 13 00:00:38,200 --> 00:00:39,520 Now in JavaScript, 14 00:00:39,520 --> 00:00:42,870 there is type conversion and type coercion. 15 00:00:42,870 --> 00:00:46,050 So they sound very similar, but are different. 16 00:00:46,050 --> 00:00:48,650 So type conversion is when we manually 17 00:00:48,650 --> 00:00:51,480 convert from one type to another. 18 00:00:51,480 --> 00:00:54,790 On the other hand type coercion is when JavaScript 19 00:00:54,790 --> 00:00:58,610 automatically converts types behind the scenes for us. 20 00:00:58,610 --> 00:01:01,070 So that's necessary in some situation, 21 00:01:01,070 --> 00:01:02,710 but it happens implicitly, 22 00:01:02,710 --> 00:01:05,880 completely hidden from us. Okay? 23 00:01:05,880 --> 00:01:08,060 So let's start with type conversion, 24 00:01:08,060 --> 00:01:10,730 which remember, is when we explicitly 25 00:01:10,730 --> 00:01:13,800 want to convert from one type to another. 26 00:01:13,800 --> 00:01:16,120 So let's say that we have an input field 27 00:01:16,120 --> 00:01:20,240 on a webpage for the user to input their birth year. 28 00:01:20,240 --> 00:01:24,593 And these inputs from input fields usually come as strings. 29 00:01:26,300 --> 00:01:29,710 So let's say that the input year 30 00:01:29,710 --> 00:01:32,700 that we get from the user interface 31 00:01:32,700 --> 00:01:36,240 is a string with the value 1991. 32 00:01:36,240 --> 00:01:39,490 But now if we want to do some calculations with this, 33 00:01:39,490 --> 00:01:41,480 this won't really work. 34 00:01:41,480 --> 00:01:42,830 So let me show that to you. 35 00:01:43,780 --> 00:01:45,113 So console dot log, 36 00:01:46,120 --> 00:01:50,700 let's say input year and now plus 18. 37 00:01:50,700 --> 00:01:53,780 And so like this, we can calculate in what year 38 00:01:53,780 --> 00:01:56,250 the person will become of full age. 39 00:01:56,250 --> 00:01:59,200 Now remember that when we have a string 40 00:01:59,200 --> 00:02:01,370 and add something to the string, 41 00:02:01,370 --> 00:02:04,430 it will basically concatenate a strings. 42 00:02:04,430 --> 00:02:06,810 So we can't expect that this 43 00:02:06,810 --> 00:02:10,010 actually adds 18 to this number here 44 00:02:10,010 --> 00:02:11,253 because it's a string. 45 00:02:12,260 --> 00:02:15,030 So let me show you what I mean with that. 46 00:02:15,030 --> 00:02:17,810 And so indeed, we now get a string 47 00:02:17,810 --> 00:02:20,950 which basically contains the 1991 string 48 00:02:20,950 --> 00:02:24,920 and then the 18 as well. Okay? 49 00:02:24,920 --> 00:02:27,410 So we need a way of fixing this, 50 00:02:27,410 --> 00:02:29,000 which means that we need a way 51 00:02:29,000 --> 00:02:32,640 of converting this string to a number. 52 00:02:32,640 --> 00:02:35,363 So let me actually log that here first. 53 00:02:36,400 --> 00:02:38,260 So console dot log, 54 00:02:38,260 --> 00:02:39,970 and then the way we convert 55 00:02:39,970 --> 00:02:42,480 this string to a number is by using 56 00:02:43,500 --> 00:02:45,580 the built in number of function. 57 00:02:45,580 --> 00:02:50,580 So we write number, then parenthesis and then input here, 58 00:02:51,060 --> 00:02:53,690 and we will learn exactly what a function is 59 00:02:53,690 --> 00:02:57,640 and why it works this way in the next section. 60 00:02:57,640 --> 00:02:59,310 For now, just know that 61 00:02:59,310 --> 00:03:02,430 we can basically convert strings to numbers 62 00:03:02,430 --> 00:03:04,730 by using this function, 63 00:03:04,730 --> 00:03:08,770 which we execute using these parenthesis here. 64 00:03:08,770 --> 00:03:11,970 So we have this parenthesis inside these parenthesis now, 65 00:03:11,970 --> 00:03:13,883 but don't get confused by that. 66 00:03:14,890 --> 00:03:18,230 So doing this operation here 67 00:03:18,230 --> 00:03:20,773 will then return the string as a number. 68 00:03:21,640 --> 00:03:22,990 So let me show that to you. 69 00:03:24,200 --> 00:03:26,660 And so indeed now we get 1991 70 00:03:26,660 --> 00:03:29,930 here in this pink color basically. 71 00:03:29,930 --> 00:03:32,840 And the colors might change throughout the time. 72 00:03:32,840 --> 00:03:34,880 By the time I record this video, 73 00:03:34,880 --> 00:03:39,320 numbers are pink and strings are just this white, 74 00:03:39,320 --> 00:03:40,920 let me actually print them both. 75 00:03:43,700 --> 00:03:48,140 And so that should make it really visible that, yeah. 76 00:03:48,140 --> 00:03:52,580 The first one is a number, the second one is a string. Okay. 77 00:03:52,580 --> 00:03:55,690 But now one thing that's really important to note here 78 00:03:55,690 --> 00:03:59,560 is that the original value, is actually not converted. 79 00:03:59,560 --> 00:04:02,730 So the input year variable itself. 80 00:04:02,730 --> 00:04:06,080 So this one is still a string, right? 81 00:04:06,080 --> 00:04:08,860 It still holds the variable 1991 82 00:04:08,860 --> 00:04:11,460 as a string and not as a number. 83 00:04:11,460 --> 00:04:14,040 That's why down here in this log 84 00:04:14,040 --> 00:04:15,980 where we do this calculation, 85 00:04:15,980 --> 00:04:18,220 the result is still this string. 86 00:04:18,220 --> 00:04:21,420 Because again, the original input year variable 87 00:04:21,420 --> 00:04:22,930 is still a string. 88 00:04:22,930 --> 00:04:24,350 Using this number function, 89 00:04:24,350 --> 00:04:27,563 will simply give to us a converted version. 90 00:04:28,590 --> 00:04:31,223 So if you want to perform this calculation, 91 00:04:32,370 --> 00:04:35,400 we need to use number here as well. 92 00:04:35,400 --> 00:04:37,930 And so this will now convert a number 93 00:04:37,930 --> 00:04:41,450 and then to that number, the 18 will be added. 94 00:04:41,450 --> 00:04:43,240 And so now we should end up 95 00:04:43,240 --> 00:04:46,633 with something like 2009, I guess, 96 00:04:47,500 --> 00:04:49,970 and indeed that's right. 97 00:04:49,970 --> 00:04:52,700 But now what if we're trying to convert something 98 00:04:52,700 --> 00:04:55,263 to a number that is impossible to convert. 99 00:04:56,380 --> 00:04:59,750 Let's try that with a string 100 00:04:59,750 --> 00:05:01,970 that doesn't really contain a number. 101 00:05:01,970 --> 00:05:06,520 So let's try to console log, converting to a number, 102 00:05:06,520 --> 00:05:09,100 the string Jonas. 103 00:05:09,100 --> 00:05:11,360 So JavaScript, we'll look at a string, 104 00:05:11,360 --> 00:05:13,810 will try to convert it to a number, 105 00:05:13,810 --> 00:05:15,470 but it won't really work. 106 00:05:15,470 --> 00:05:19,250 So what do we get instead? we get this non. 107 00:05:19,250 --> 00:05:21,870 Which stands for not a number. 108 00:05:21,870 --> 00:05:25,040 So a JavaScript gives us this, not a number value 109 00:05:25,040 --> 00:05:27,570 whenever an operation that involves numbers 110 00:05:27,570 --> 00:05:30,670 fails to produce a new number. 111 00:05:30,670 --> 00:05:35,210 So basically not a number actually means invalid number. 112 00:05:35,210 --> 00:05:37,090 It's not really not a number. 113 00:05:37,090 --> 00:05:39,143 And let me actually prove that to you. 114 00:05:41,140 --> 00:05:46,140 So we can check the type of NaN, so not a number. 115 00:05:48,070 --> 00:05:52,400 And as we will see the weird result of this 116 00:05:52,400 --> 00:05:56,230 is that the type of not a number is actually number. 117 00:05:56,230 --> 00:05:58,830 And so again, not a number 118 00:05:58,830 --> 00:06:01,500 actually means an invalid number. 119 00:06:01,500 --> 00:06:04,623 It's still a number of somehow, but it's an invalid one. 120 00:06:05,908 --> 00:06:07,710 And so again we get not a number 121 00:06:07,710 --> 00:06:10,520 whenever an operation involving numbers, 122 00:06:10,520 --> 00:06:14,620 fails to give us a new number. Okay. 123 00:06:14,620 --> 00:06:17,190 So that is converting strings to numbers, 124 00:06:17,190 --> 00:06:19,733 but of course we can also do the opposite. 125 00:06:22,150 --> 00:06:25,280 It's a little bit less important, I would say, 126 00:06:25,280 --> 00:06:27,283 but I still want to show it to you. 127 00:06:28,970 --> 00:06:30,920 So to do it the other way around, 128 00:06:30,920 --> 00:06:33,410 we use this string function. 129 00:06:33,410 --> 00:06:36,780 And that's quite straightforward, right? 130 00:06:36,780 --> 00:06:38,030 Just keep in mind that 131 00:06:38,030 --> 00:06:41,110 we need to really start it with a capital S 132 00:06:41,110 --> 00:06:43,010 just like here the number function, 133 00:06:43,010 --> 00:06:45,530 needs to start with a capital N. 134 00:06:45,530 --> 00:06:47,103 Otherwise it's not gonna work. 135 00:06:48,760 --> 00:06:50,618 So we get 23. 136 00:06:50,618 --> 00:06:54,130 and remember that whenever the value here is white, 137 00:06:54,130 --> 00:06:56,330 then it means it is a string. 138 00:06:56,330 --> 00:06:57,780 So it looks kind of the same. 139 00:06:59,150 --> 00:07:04,120 Let's again, log both just to make this point. 140 00:07:04,120 --> 00:07:05,890 So the pink one is the value 141 00:07:05,890 --> 00:07:08,230 that actually has the number type, 142 00:07:08,230 --> 00:07:10,353 and this one has the string type. 143 00:07:11,560 --> 00:07:14,520 Okay. So again, this one is not as important, 144 00:07:14,520 --> 00:07:16,730 but I still wanted to mention it. 145 00:07:16,730 --> 00:07:20,570 Now, JavaScript can only convert two, three types. 146 00:07:20,570 --> 00:07:22,800 So we can convert to a number, 147 00:07:22,800 --> 00:07:25,950 to a string or to a Boolean. 148 00:07:25,950 --> 00:07:27,700 But we cannot, for example, 149 00:07:27,700 --> 00:07:31,200 convert something to undefined or to no. 150 00:07:31,200 --> 00:07:33,410 That doesn't make a lot of sense. 151 00:07:33,410 --> 00:07:35,730 Now here, we only converted to numbers 152 00:07:35,730 --> 00:07:38,550 and to strings, but not two Booleans. 153 00:07:38,550 --> 00:07:41,900 And that's because Booleans behave in a special way. 154 00:07:41,900 --> 00:07:44,870 And for that reason, there is a separate lecture coming up 155 00:07:44,870 --> 00:07:47,973 on so-called "truthy" and "faulty" values. 156 00:07:48,920 --> 00:07:51,160 Great. So that is type conversion 157 00:07:51,160 --> 00:07:55,040 where we do manually convert from one type to another. 158 00:07:55,040 --> 00:07:56,430 However, in practice, 159 00:07:56,430 --> 00:07:58,770 we rarely have to do it manually 160 00:07:58,770 --> 00:08:01,490 because JavaScript actually does type coercion 161 00:08:01,490 --> 00:08:04,470 automatically for us in many situations. 162 00:08:04,470 --> 00:08:06,440 So let's talk about that now. 163 00:08:06,440 --> 00:08:09,983 And let's just separate this here with some comments, 164 00:08:11,320 --> 00:08:15,950 so type conversion and then type coercion. 165 00:08:20,310 --> 00:08:22,820 So basically type coercion happens 166 00:08:22,820 --> 00:08:25,010 whenever an operator is dealing 167 00:08:25,010 --> 00:08:28,000 with two values that have different types. 168 00:08:28,000 --> 00:08:28,980 So in that case, 169 00:08:28,980 --> 00:08:31,440 JavaScript will then, behind the scenes, 170 00:08:31,440 --> 00:08:35,030 convert one of the values to match the other value 171 00:08:35,030 --> 00:08:35,950 so that in the end, 172 00:08:35,950 --> 00:08:38,220 the operation can be executed. 173 00:08:38,220 --> 00:08:40,760 And actually we already saw that happening, 174 00:08:40,760 --> 00:08:41,963 if you think about this. 175 00:08:42,950 --> 00:08:47,210 So let me show that to you, 176 00:08:47,210 --> 00:08:48,973 starting with strings. 177 00:08:49,920 --> 00:08:51,660 Remember how we did this. 178 00:08:51,660 --> 00:08:55,230 I am, and then a number, let's say 23 179 00:08:56,930 --> 00:08:59,907 and then another plus, years old. 180 00:09:01,570 --> 00:09:04,140 So we already know that this is gonna produce a string 181 00:09:04,140 --> 00:09:07,060 that says I am 23 years old. 182 00:09:07,060 --> 00:09:09,240 But how does that actually work? 183 00:09:09,240 --> 00:09:11,410 Because 23 is a number. 184 00:09:11,410 --> 00:09:13,930 So we have different types here, right? 185 00:09:13,930 --> 00:09:17,263 We have a string, a number and another string. 186 00:09:18,340 --> 00:09:23,120 So let's check. And indeed, that is what happens. 187 00:09:23,120 --> 00:09:26,190 And it works this way because of type of coercion. 188 00:09:26,190 --> 00:09:29,920 So in JavaScript, the plus operator that we used here 189 00:09:29,920 --> 00:09:32,460 triggers a coercion to strings. 190 00:09:32,460 --> 00:09:34,190 And so whenever there is an operation 191 00:09:34,190 --> 00:09:36,220 between a string and the number, 192 00:09:36,220 --> 00:09:39,550 the number will be converted to a string. 193 00:09:39,550 --> 00:09:41,150 So thanks to type coercion, 194 00:09:41,150 --> 00:09:45,803 writing this would be exactly the same as writing this. 195 00:09:50,500 --> 00:09:51,960 Right? Because again, 196 00:09:51,960 --> 00:09:55,770 the plus operator will convert numbers to strings. 197 00:09:55,770 --> 00:09:58,600 And the same actually happens in template literals. 198 00:09:58,600 --> 00:10:00,800 It also takes all the number of values 199 00:10:00,800 --> 00:10:03,490 and also converts them to strings. 200 00:10:03,490 --> 00:10:06,910 Now, if JavaScript did not have automatic type coercion, 201 00:10:06,910 --> 00:10:09,170 like many other languages don't, 202 00:10:09,170 --> 00:10:11,720 then we would have to manually do this 203 00:10:11,720 --> 00:10:13,890 like we just learned before. 204 00:10:13,890 --> 00:10:17,850 Then we would have to do string 23. 205 00:10:17,850 --> 00:10:21,220 And then this would be the only way that this would work. 206 00:10:21,220 --> 00:10:24,370 But luckily for us, Javascript has type coercion 207 00:10:24,370 --> 00:10:26,330 and so this will happen completely 208 00:10:26,330 --> 00:10:28,690 automatically behind the scenes. 209 00:10:28,690 --> 00:10:31,320 Now, actually not all the operators 210 00:10:31,320 --> 00:10:33,840 do type coercion to string. 211 00:10:33,840 --> 00:10:35,583 So let me show you something else. 212 00:10:37,120 --> 00:10:42,120 So if we do 23 to string, minus 10 to string, minus three, 213 00:10:45,910 --> 00:10:47,823 what do you think will happen now? 214 00:10:48,930 --> 00:10:53,883 So, let's actually check and it gives us 10. 215 00:10:54,770 --> 00:10:57,600 So what happened here? It looks like this time, 216 00:10:57,600 --> 00:11:01,230 JavaScript converted the strings, to numbers. 217 00:11:01,230 --> 00:11:03,750 And indeed, that's why we get 10, 218 00:11:03,750 --> 00:11:08,750 because 23 minus 10 is 13, minus three is 10. 219 00:11:08,760 --> 00:11:11,650 And so what this means is that the minus operator 220 00:11:11,650 --> 00:11:14,270 actually triggers the opposite conversion. 221 00:11:14,270 --> 00:11:17,630 So in this case, strings are converted to numbers 222 00:11:17,630 --> 00:11:19,710 and not the other way around. 223 00:11:19,710 --> 00:11:23,120 So instead if we use the plus, 224 00:11:23,120 --> 00:11:24,970 what do you think is going to happen? 225 00:11:26,500 --> 00:11:29,040 10 to three is converted to a string 226 00:11:29,040 --> 00:11:31,403 and then the three strings are concatenated. 227 00:11:32,570 --> 00:11:35,380 Okay? So this is a very important distinction 228 00:11:35,380 --> 00:11:38,700 to keep in mind because this actually confuses 229 00:11:38,700 --> 00:11:41,913 many JavaScript beginners when they don't know about this. 230 00:11:44,032 --> 00:11:45,900 So let's try another one here. 231 00:11:45,900 --> 00:11:50,900 And I'm again, using 23 the string, times two the string. 232 00:11:51,790 --> 00:11:53,630 And again, you will see that these values 233 00:11:53,630 --> 00:11:57,303 are gonna be converted to numbers before. 234 00:11:58,200 --> 00:12:00,780 And indeed, that's why we get 46 235 00:12:00,780 --> 00:12:03,830 because both of them are now converted to numbers. 236 00:12:03,830 --> 00:12:05,910 'cause that's the only way that 237 00:12:05,910 --> 00:12:08,920 the multiplier operator can work. 238 00:12:08,920 --> 00:12:11,773 And the same of course is true for dividing. 239 00:12:14,220 --> 00:12:18,600 Now, one final example is a logical operator. 240 00:12:18,600 --> 00:12:22,810 So let's check 23, the string again, 241 00:12:22,810 --> 00:12:25,383 greater than 18, the string. 242 00:12:27,710 --> 00:12:30,293 So, do you think this is going to work? 243 00:12:32,040 --> 00:12:33,750 Yes, it does. 244 00:12:33,750 --> 00:12:37,730 So 23, the string is still greater than 18 the string, 245 00:12:37,730 --> 00:12:40,740 because once more JavaScript will convert strings 246 00:12:40,740 --> 00:12:42,910 to numbers automatically. 247 00:12:42,910 --> 00:12:45,270 The only way in which it doesn't do that 248 00:12:45,270 --> 00:12:48,720 is the plus operator and plus operator again, 249 00:12:48,720 --> 00:12:50,490 it happens the other way around. 250 00:12:50,490 --> 00:12:53,830 All the numbers are converted to strings. 251 00:12:53,830 --> 00:12:57,723 Okay. I hope that distinction is now pretty clear. 252 00:12:58,580 --> 00:13:00,850 So just to make sure that you really got it, 253 00:13:00,850 --> 00:13:04,660 let's play a game called "guess the output." 254 00:13:04,660 --> 00:13:06,853 So that's a game I just made up. 255 00:13:07,800 --> 00:13:10,390 So I want you to guess what happens here. 256 00:13:10,390 --> 00:13:15,390 So I will write one plus one like this, 257 00:13:16,560 --> 00:13:21,560 and then we say n equals n minus one again. 258 00:13:28,750 --> 00:13:31,770 So what do you think n we'll look like 259 00:13:31,770 --> 00:13:33,920 when we log it to the console? 260 00:13:33,920 --> 00:13:36,670 So essentially we start with one the string, 261 00:13:36,670 --> 00:13:39,363 then we add one, and then we subtract one. 262 00:13:41,020 --> 00:13:44,280 So take a moment to think what this should look like. 263 00:13:44,280 --> 00:13:49,280 And then let's take a look at the solution and it is 10. 264 00:13:50,660 --> 00:13:52,900 So that's a bit counterintuitive, 265 00:13:52,900 --> 00:13:55,800 but according to the rules that we just learned before, 266 00:13:55,800 --> 00:13:57,420 it actually makes sense. 267 00:13:57,420 --> 00:13:59,150 So here in the first line, 268 00:13:59,150 --> 00:14:03,900 one plus one will actually turn out to be 11, the string, 269 00:14:03,900 --> 00:14:07,510 because we have one string here and then the plus operator 270 00:14:07,510 --> 00:14:10,080 will automatically convert a number to a string. 271 00:14:10,080 --> 00:14:14,810 And so the result of this one is the string, 11. 272 00:14:14,810 --> 00:14:17,430 But then here we have the minus operator 273 00:14:17,430 --> 00:14:19,080 and in the minus operator, 274 00:14:19,080 --> 00:14:21,930 whenever we have a string or more strings, 275 00:14:21,930 --> 00:14:24,170 it will then convert it to a number. 276 00:14:24,170 --> 00:14:27,120 And so here, the string 11 will be converted 277 00:14:27,120 --> 00:14:32,113 to 11 the number and then 11 minus one is of course 10. 278 00:14:33,760 --> 00:14:35,990 Okay. Let's do one or two more 279 00:14:35,990 --> 00:14:38,100 and let's do it in the console here. 280 00:14:38,100 --> 00:14:40,200 And we can actually clear the console 281 00:14:40,200 --> 00:14:44,493 of all this clutter here by clicking on clear console. 282 00:14:45,380 --> 00:14:48,100 Okay? So let's do two plus three 283 00:14:48,100 --> 00:14:52,237 plus four plus five the string. 284 00:14:52,237 --> 00:14:54,550 And now you actually can really guess 285 00:14:54,550 --> 00:14:57,070 because we can already see the solution there. 286 00:14:57,070 --> 00:15:00,610 Okay? So let's try to understand what happened here. 287 00:15:00,610 --> 00:15:03,900 So we start here with two plus three, 288 00:15:03,900 --> 00:15:08,630 which makes five, then five plus four makes nine. 289 00:15:08,630 --> 00:15:12,420 And then we end up with nine plus five the string. 290 00:15:12,420 --> 00:15:15,480 And then as you already know, since we have a string, 291 00:15:15,480 --> 00:15:18,610 the plus operator will convert the other operand, 292 00:15:18,610 --> 00:15:20,360 which is nine, also to a string. 293 00:15:20,360 --> 00:15:23,963 And then we end up with nine, five as a string. 294 00:15:24,860 --> 00:15:29,860 Okay. And another one, 10 minus four minus three, 295 00:15:33,500 --> 00:15:35,740 And keep in mind that these are strings, 296 00:15:35,740 --> 00:15:39,480 and then minus the real number two, 297 00:15:39,480 --> 00:15:42,280 but then plus five to string. 298 00:15:42,280 --> 00:15:44,310 So again, this looks very weird, 299 00:15:44,310 --> 00:15:47,240 but the logic is kind of the same as before. 300 00:15:47,240 --> 00:15:51,000 So we have two values that are subtracted, 301 00:15:51,000 --> 00:15:54,520 and so JavaScript will convert in both to numbers. 302 00:15:54,520 --> 00:15:57,250 And so the result of this is six, 303 00:15:57,250 --> 00:16:01,110 then six minus three is three, 304 00:16:01,110 --> 00:16:03,510 then three minus two is one. 305 00:16:03,510 --> 00:16:06,600 And then we end up with the same situation as before. 306 00:16:06,600 --> 00:16:09,700 So this one will be converted to a string 307 00:16:09,700 --> 00:16:11,500 and then we end up with a string 15. 308 00:16:13,883 --> 00:16:15,770 Okay. And with that, 309 00:16:15,770 --> 00:16:18,610 I think a this would be pretty clear now. 310 00:16:18,610 --> 00:16:19,750 And you might be wondering, 311 00:16:19,750 --> 00:16:22,000 why we're talking so much about this. 312 00:16:22,000 --> 00:16:23,370 But it's really important 313 00:16:23,370 --> 00:16:25,870 that you know about this right from the start 314 00:16:25,870 --> 00:16:29,650 so that you can write your code with all of this in mind. 315 00:16:29,650 --> 00:16:32,590 Now, many people actually don't like type coercion 316 00:16:32,590 --> 00:16:37,090 and think that it's a bad practice to rely on type coercion. 317 00:16:37,090 --> 00:16:40,477 One reason for that is that type coercion can in fact, 318 00:16:40,477 --> 00:16:44,740 introduce many unexpected bugs into our program. 319 00:16:44,740 --> 00:16:46,610 However, this only happens 320 00:16:46,610 --> 00:16:49,230 when we don't really know what we're doing. 321 00:16:49,230 --> 00:16:51,100 So when we don't know about the stuff 322 00:16:51,100 --> 00:16:52,360 that it just showed you. 323 00:16:52,360 --> 00:16:54,490 Because if you know, then it's way easier 324 00:16:54,490 --> 00:16:56,550 to avoid these errors. 325 00:16:56,550 --> 00:17:00,760 So in my opinion, coercion is actually a great mechanism 326 00:17:00,760 --> 00:17:03,630 that is going to allow us to write a lot less code 327 00:17:03,630 --> 00:17:06,860 and also to write more readable code. 328 00:17:06,860 --> 00:17:08,930 So really make sure to take some time 329 00:17:08,930 --> 00:17:11,450 to understand how type coercion works 330 00:17:11,450 --> 00:17:14,053 and then just embrace it in your code. 24516

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