All language subtitles for 20. Working With Strings - Part 1

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 1 00:00:01,310 --> 00:00:02,930 Over the next few lectures, 2 2 00:00:02,930 --> 00:00:06,280 we will learn how to work with strings 3 3 00:00:06,280 --> 00:00:08,130 and so we're gonna be taking a look 4 4 00:00:08,130 --> 00:00:11,230 at a couple of useful string methods. 5 5 00:00:11,230 --> 00:00:14,160 Now I could have divided this part about strings 6 6 00:00:14,160 --> 00:00:15,610 into many more lectures 7 7 00:00:15,610 --> 00:00:18,780 and go really deep into some topics 8 8 00:00:18,780 --> 00:00:22,170 but I think it's best to just quickly introduce you 9 9 00:00:22,170 --> 00:00:23,950 to all the topics here 10 10 00:00:23,950 --> 00:00:26,800 and then keep using them in future projects 11 11 00:00:26,800 --> 00:00:30,700 because we work with strings all the time in JavaScript 12 12 00:00:30,700 --> 00:00:33,330 and so you will see these methods being used 13 13 00:00:33,330 --> 00:00:36,400 all the time throughout the rest of the course. 14 14 00:00:36,400 --> 00:00:39,080 Anyway, this is gonna be really fun 15 15 00:00:39,080 --> 00:00:40,653 so let's get started. 16 16 00:00:42,210 --> 00:00:43,950 And in this part of the section, 17 17 00:00:43,950 --> 00:00:46,850 we're gonna leave the restaurant theme behind 18 18 00:00:46,850 --> 00:00:51,590 and work on stuff related to airplanes and airlines. 19 19 00:00:51,590 --> 00:00:54,360 So that's something that I really like. 20 20 00:00:54,360 --> 00:00:59,360 So let's now start by creating an airline variable here 21 21 00:01:00,530 --> 00:01:03,283 and I'm using TAP Air Portugal. 22 22 00:01:06,920 --> 00:01:10,740 All right, and if you want, you can use some other one 23 23 00:01:10,740 --> 00:01:12,163 from your country maybe, 24 24 00:01:13,100 --> 00:01:16,340 but then you might have to adapt the examples. 25 25 00:01:16,340 --> 00:01:17,540 So as a plane, 26 26 00:01:17,540 --> 00:01:20,843 I'm using a string A320. 27 27 00:01:21,710 --> 00:01:22,543 Okay. 28 28 00:01:22,543 --> 00:01:25,300 And now let's do some stuff with this. 29 29 00:01:25,300 --> 00:01:26,600 So just like in arrays, 30 30 00:01:26,600 --> 00:01:30,923 we can get the character of a string at a certain position. 31 31 00:01:31,970 --> 00:01:36,290 So for example, plane at position zero 32 32 00:01:36,290 --> 00:01:38,750 should be A, right? 33 33 00:01:38,750 --> 00:01:41,550 So let's load this here. 34 34 00:01:41,550 --> 00:01:42,777 And indeed we got A 35 35 00:01:43,775 --> 00:01:46,363 and the same of course for all the other letters, 36 36 00:01:47,450 --> 00:01:49,990 one, two for example. 37 37 00:01:49,990 --> 00:01:53,730 And so indeed you now get three and two here 38 38 00:01:53,730 --> 00:01:56,370 but both of them are still strings. 39 39 00:01:56,370 --> 00:01:58,570 So if we wanted them to be numbers, 40 40 00:01:58,570 --> 00:02:03,036 we would have to convert them and we can do the same 41 41 00:02:03,036 --> 00:02:05,333 directly on a string. 42 42 00:02:06,440 --> 00:02:11,150 So we can just write a string and let's write B727 now, 43 43 00:02:11,150 --> 00:02:15,263 which is a Boeing and so this works the same. 44 44 00:02:16,630 --> 00:02:18,910 Okay so now we got to the B, 45 45 00:02:18,910 --> 00:02:22,133 we can also read the length property of strings, 46 46 00:02:23,369 --> 00:02:25,590 just like we can in arrays. 47 47 00:02:25,590 --> 00:02:29,470 So let's say airline.length. 48 48 00:02:29,470 --> 00:02:32,513 And again we can also do that directly on the string. 49 49 00:02:36,590 --> 00:02:37,840 Okay. 50 50 00:02:37,840 --> 00:02:38,673 There you go. 51 51 00:02:39,520 --> 00:02:42,330 Next up let's talk about methods. 52 52 00:02:42,330 --> 00:02:45,560 So again comparing strings to arrays here, 53 53 00:02:45,560 --> 00:02:48,180 strings also have methods 54 54 00:02:48,180 --> 00:02:52,170 and some of them are quite similar to the array methods. 55 55 00:02:52,170 --> 00:02:54,713 So one of them is the index off. 56 56 00:02:56,250 --> 00:02:58,870 So I believe we talked about this one 57 57 00:02:58,870 --> 00:03:00,795 when we talked about a array. 58 58 00:03:00,795 --> 00:03:02,760 And so this one works the same way, 59 59 00:03:02,760 --> 00:03:05,754 so we can get the position in which a certain letter 60 60 00:03:05,754 --> 00:03:07,770 is in the string. 61 61 00:03:07,770 --> 00:03:09,343 So let's use r here. 62 62 00:03:11,190 --> 00:03:14,490 And so here we now get to position number six. 63 63 00:03:14,490 --> 00:03:19,490 And so let's see zero, one, two, three, four, five and six. 64 64 00:03:21,740 --> 00:03:24,713 Okay and that's where the six here comes from. 65 65 00:03:25,830 --> 00:03:30,130 So you'll see that strings are also zero-based right? 66 66 00:03:30,130 --> 00:03:32,973 And we could observe that already up here. 67 67 00:03:33,890 --> 00:03:37,290 Now this here will only give us the first occurrence 68 68 00:03:37,290 --> 00:03:40,090 but sometimes we might need the last one. 69 69 00:03:40,090 --> 00:03:42,323 And so then we can use lastIndexOf, 70 70 00:03:45,860 --> 00:03:49,940 lastIndexOf and so that is 10. 71 71 00:03:49,940 --> 00:03:53,040 So this one was six then seven, 72 72 00:03:53,040 --> 00:03:56,780 because the space of course also counts as a character, 73 73 00:03:56,780 --> 00:03:59,523 then eight, nine, and this is position 10. 74 74 00:04:00,840 --> 00:04:05,683 Alright and we can also search for entire words. 75 75 00:04:10,260 --> 00:04:14,313 For example, for the occurrence of Portugal, 76 76 00:04:15,280 --> 00:04:17,530 and so that is at position eight, 77 77 00:04:17,530 --> 00:04:20,040 and this one is actually case sensitive. 78 78 00:04:20,040 --> 00:04:22,460 So if I search with lowercase, 79 79 00:04:22,460 --> 00:04:24,080 then we get minus one, 80 80 00:04:24,080 --> 00:04:26,350 because this can now not be found 81 81 00:04:26,350 --> 00:04:29,010 in this airline's string. 82 82 00:04:29,010 --> 00:04:34,010 All right now, what can we actually do with these indexes? 83 83 00:04:34,180 --> 00:04:36,050 Why are they useful? 84 84 00:04:36,050 --> 00:04:39,610 Well, one good use case is to extract part 85 85 00:04:39,610 --> 00:04:42,800 of a string using the slice method 86 86 00:04:42,800 --> 00:04:46,390 and a slice method needs indexes as arguments. 87 87 00:04:46,390 --> 00:04:49,280 And so therefore sometimes it can be very useful 88 88 00:04:49,280 --> 00:04:52,900 to first figure out the index of part of a string 89 89 00:04:52,900 --> 00:04:54,940 to then extract that. 90 90 00:04:54,940 --> 00:04:59,623 So let's see how the slice method works. 91 91 00:05:02,470 --> 00:05:06,140 So airline and then dot slice, 92 92 00:05:06,140 --> 00:05:09,583 and let's write four and see the result here. 93 93 00:05:10,810 --> 00:05:15,090 All right so the result is Air Portugal. 94 94 00:05:15,090 --> 00:05:17,870 And the reason for that is that this here 95 95 00:05:17,870 --> 00:05:19,660 is the begin parameter. 96 96 00:05:19,660 --> 00:05:21,930 So basically it's the position at which 97 97 00:05:21,930 --> 00:05:23,763 the extraction will start. 98 98 00:05:24,960 --> 00:05:27,970 And remember that this is zero based. 99 99 00:05:27,970 --> 00:05:31,870 So zero, one, two, three, and four. 100 100 00:05:31,870 --> 00:05:34,580 So four happens to be this A. 101 101 00:05:34,580 --> 00:05:38,663 And so this is where the slice method starts to extract. 102 102 00:05:38,663 --> 00:05:41,502 And this result that we get here, 103 103 00:05:41,502 --> 00:05:45,250 so just this here is called a substring 104 104 00:05:45,250 --> 00:05:48,981 because it's just a part of the original string. 105 105 00:05:48,981 --> 00:05:52,870 Now this does not change the underlying string okay? 106 106 00:05:52,870 --> 00:05:54,600 Just keep that in mind. 107 107 00:05:54,600 --> 00:05:58,410 That's because it's actually impossible to mutate strings. 108 108 00:05:58,410 --> 00:06:00,000 They are primitives, right? 109 109 00:06:00,000 --> 00:06:02,880 So if we wanted to use this string 110 110 00:06:02,880 --> 00:06:06,475 now we would have to store it first into some variable 111 111 00:06:06,475 --> 00:06:08,800 or some data structure. 112 112 00:06:08,800 --> 00:06:13,170 Okay, so this method here and all the other ones 113 113 00:06:13,170 --> 00:06:17,230 that we're gonna talk about always return a new string. 114 114 00:06:17,230 --> 00:06:20,680 Okay and so that's why we can then lock that result 115 115 00:06:20,680 --> 00:06:22,283 to the console like this. 116 116 00:06:23,240 --> 00:06:27,060 Now, besides the begin parameter that we already specified, 117 117 00:06:27,060 --> 00:06:30,630 we can also specify an end parameter. 118 118 00:06:30,630 --> 00:06:32,733 So let's try seven and see the result. 119 119 00:06:33,850 --> 00:06:36,390 And so now we only got air. 120 120 00:06:36,390 --> 00:06:41,390 Okay so this one is four, then five, six 121 121 00:06:41,660 --> 00:06:43,560 and seven is the space here. 122 122 00:06:43,560 --> 00:06:46,460 And so what this means is that the end value 123 123 00:06:46,460 --> 00:06:49,650 is actually not included in the string. 124 124 00:06:49,650 --> 00:06:52,840 All right so basically it stops extracting 125 125 00:06:52,840 --> 00:06:55,610 before reaching index number seven. 126 126 00:06:55,610 --> 00:06:58,060 And that's really important to keep in mind. 127 127 00:06:58,060 --> 00:06:59,700 And just as a side note, 128 128 00:06:59,700 --> 00:07:02,019 the length of the extracted string 129 129 00:07:02,019 --> 00:07:05,550 is always going to be end minus beginning. 130 130 00:07:05,550 --> 00:07:07,910 So seven minus four is three. 131 131 00:07:07,910 --> 00:07:10,710 And so that's the length here of air. 132 132 00:07:10,710 --> 00:07:14,150 All right, okay. 133 133 00:07:14,150 --> 00:07:16,070 Now up until this point, 134 134 00:07:16,070 --> 00:07:19,350 we have always just hard-coded these values, 135 135 00:07:19,350 --> 00:07:22,333 but ofcourse many times we don't even know the string 136 136 00:07:22,333 --> 00:07:24,508 that we receive yet okay? 137 137 00:07:24,508 --> 00:07:28,472 And so let's now try to extract just the first word 138 138 00:07:28,472 --> 00:07:30,147 of this string here, 139 139 00:07:30,147 --> 00:07:33,490 but without knowing any of the indexes. 140 140 00:07:33,490 --> 00:07:36,450 And so that's where this IndexOf, 141 141 00:07:36,450 --> 00:07:40,100 and lastIndexOf here become really important. 142 142 00:07:40,100 --> 00:07:43,310 So basically that we do not have to hard code 143 143 00:07:43,310 --> 00:07:44,823 these values here. 144 144 00:07:46,100 --> 00:07:50,820 All right so let's try to extract that first word 145 145 00:07:50,820 --> 00:07:51,803 as I was saying. 146 146 00:07:54,650 --> 00:07:56,910 Airline.slice 147 147 00:07:56,910 --> 00:07:59,790 and now we need to figure out the index. 148 148 00:07:59,790 --> 00:08:04,790 And now if we want the first word we need to start at zero, 149 149 00:08:04,840 --> 00:08:07,850 but what about the end parameter? 150 150 00:08:07,850 --> 00:08:12,620 Well we want to extract until this space here basically. 151 151 00:08:12,620 --> 00:08:14,823 So let's find the index of that. 152 152 00:08:16,060 --> 00:08:17,690 So airline.indexOf, 153 153 00:08:19,680 --> 00:08:22,751 and remember that this will be the first occurrence 154 154 00:08:22,751 --> 00:08:26,793 and so indeed we get top. 155 155 00:08:28,640 --> 00:08:32,040 All right and now let's do the opposite extracting 156 156 00:08:32,040 --> 00:08:36,890 the last word, so slice. 157 157 00:08:36,890 --> 00:08:38,070 And so for the last word, 158 158 00:08:38,070 --> 00:08:41,323 we want to start at the last space here. 159 159 00:08:42,410 --> 00:08:45,173 Okay that's why we have lastIndexOf. 160 160 00:08:46,760 --> 00:08:48,740 So airline.lastIndexOf 161 161 00:08:50,800 --> 00:08:53,200 and again searching for the space. 162 162 00:08:53,200 --> 00:08:55,560 And now we don't need the end parameter 163 163 00:08:55,560 --> 00:08:57,554 because then if we don't specify it, 164 164 00:08:57,554 --> 00:09:00,643 it will simply extract everything until the end. 165 165 00:09:01,720 --> 00:09:02,910 Okay. 166 166 00:09:02,910 --> 00:09:06,600 But actually now the space is also included here. 167 167 00:09:06,600 --> 00:09:09,893 And so we just need to add plus one, 168 168 00:09:11,270 --> 00:09:12,573 and now we're good. 169 169 00:09:14,550 --> 00:09:18,550 So that's the fundamentals of the slice method, 170 170 00:09:18,550 --> 00:09:21,183 but we can do even more with it. 171 171 00:09:22,380 --> 00:09:25,640 So let's go again here. 172 172 00:09:25,640 --> 00:09:29,740 And we can even define a negative begin argument. 173 173 00:09:29,740 --> 00:09:31,240 Like this for example, 174 174 00:09:31,240 --> 00:09:34,740 and then it will start counting from the end. 175 175 00:09:34,740 --> 00:09:37,513 Or actually start extracting from the end. 176 176 00:09:38,710 --> 00:09:42,500 So these are the last two letters from Portugal. 177 177 00:09:42,500 --> 00:09:45,830 And finally let's duplicate this one again. 178 178 00:09:45,830 --> 00:09:50,053 And again I'm using this shortcut here for duplicate. 179 179 00:09:51,140 --> 00:09:52,800 Okay. 180 180 00:09:52,800 --> 00:09:57,220 So we can also specify a negative end parameter. 181 181 00:09:57,220 --> 00:09:59,490 So the beginning is back to positive, 182 182 00:09:59,490 --> 00:10:02,150 and now the end is minus one. 183 183 00:10:02,150 --> 00:10:03,900 So let's see. 184 184 00:10:03,900 --> 00:10:06,793 And so basically we started at position one. 185 185 00:10:07,640 --> 00:10:09,840 So that's why the T is cut off. 186 186 00:10:09,840 --> 00:10:12,090 And then the negative end parameter, 187 187 00:10:12,090 --> 00:10:14,523 basically cuts off the last character. 188 188 00:10:15,720 --> 00:10:17,050 All right. 189 189 00:10:17,050 --> 00:10:18,540 And so we will need all of these 190 190 00:10:18,540 --> 00:10:20,230 different combinations here, 191 191 00:10:20,230 --> 00:10:22,160 in different situations. 192 192 00:10:22,160 --> 00:10:24,580 So it's good that you know how to use them, 193 193 00:10:24,580 --> 00:10:27,093 because you will need them at some point. 194 194 00:10:28,000 --> 00:10:28,833 Okay. 195 195 00:10:28,833 --> 00:10:30,730 And now let's practice a little bit, 196 196 00:10:30,730 --> 00:10:32,250 what we just learned 197 197 00:10:32,250 --> 00:10:36,120 and write a function that receives an airplane seat 198 198 00:10:36,120 --> 00:10:37,710 and locks to the console, 199 199 00:10:37,710 --> 00:10:40,870 whether it is a middle seat or not. 200 200 00:10:40,870 --> 00:10:43,760 I hope that sounds like fun. 201 201 00:10:43,760 --> 00:10:44,803 So let's write. 202 202 00:10:46,370 --> 00:10:47,690 Check, 203 203 00:10:47,690 --> 00:10:48,830 middle, 204 204 00:10:48,830 --> 00:10:49,663 seat. 205 205 00:10:50,640 --> 00:10:54,113 And it is a function which takes in a seat. 206 206 00:10:55,290 --> 00:10:57,860 And just to illustrate it, 207 207 00:10:57,860 --> 00:11:01,200 let me start by calling this function. 208 208 00:11:01,200 --> 00:11:02,763 To check middle seat. 209 209 00:11:03,830 --> 00:11:04,830 And so typically, 210 210 00:11:04,830 --> 00:11:07,380 an airplane seat looks like this. 211 211 00:11:07,380 --> 00:11:10,400 So we have the row as a number here 212 212 00:11:10,400 --> 00:11:12,280 and then the seat itself, 213 213 00:11:12,280 --> 00:11:14,897 so that's like the column so to say, 214 214 00:11:14,897 --> 00:11:18,360 it's then a letter for example a B. 215 215 00:11:18,360 --> 00:11:19,580 So it starts from the left 216 216 00:11:19,580 --> 00:11:22,140 and goes all the way to the right side. 217 217 00:11:22,140 --> 00:11:26,550 And in small planes, like the A320, or the Boeing 737, 218 218 00:11:26,550 --> 00:11:29,160 we only have six seats in one row. 219 219 00:11:29,160 --> 00:11:33,860 And that means that B and E are the middle seats. 220 220 00:11:33,860 --> 00:11:35,680 Let me just write that here. 221 221 00:11:35,680 --> 00:11:37,023 B and E, 222 222 00:11:38,470 --> 00:11:40,550 are middle seats. 223 223 00:11:40,550 --> 00:11:42,280 So just in case you're not familiar 224 224 00:11:42,280 --> 00:11:45,100 with how these small planes works. 225 225 00:11:45,100 --> 00:11:45,933 Right. 226 226 00:11:45,933 --> 00:11:48,570 And that's also not the point here of course. 227 227 00:11:48,570 --> 00:11:50,750 So basically all we want to do is to check 228 228 00:11:50,750 --> 00:11:52,880 if the string that we receive, 229 229 00:11:52,880 --> 00:11:54,720 contains a, B or an E. 230 230 00:11:54,720 --> 00:11:56,513 And if so, then it's a middle seat. 231 231 00:11:57,760 --> 00:12:01,090 And let's call this with a couple of different strings. 232 232 00:12:01,090 --> 00:12:02,130 So 23C 233 233 00:12:07,303 --> 00:12:08,136 and 3E. 234 234 00:12:10,120 --> 00:12:14,250 All right, so what we need to do here is basically 235 235 00:12:14,250 --> 00:12:17,400 take the last character of the string 236 236 00:12:17,400 --> 00:12:21,283 and test whether it is a B or an E right? 237 237 00:12:22,510 --> 00:12:25,530 So let's get that letter from the seat. 238 238 00:12:25,530 --> 00:12:28,570 Now we'll just call it S for seat again, 239 239 00:12:28,570 --> 00:12:31,280 but I cannot repeat the same name here. 240 240 00:12:31,280 --> 00:12:33,523 So how do I do that now? 241 241 00:12:34,980 --> 00:12:37,300 Well to extract part of a string, 242 242 00:12:37,300 --> 00:12:40,796 we already know we use the slice method 243 243 00:12:40,796 --> 00:12:44,962 and how do we take the last character of a certain string? 244 244 00:12:44,962 --> 00:12:49,280 Well, we use minus one as the begin character. 245 245 00:12:49,280 --> 00:12:51,207 And then as we saw up here, 246 246 00:12:51,207 --> 00:12:55,473 it will start counting basically one from the right side. 247 247 00:12:56,400 --> 00:12:57,963 And now it's very easy. 248 248 00:12:59,540 --> 00:13:03,610 If S equals B 249 249 00:13:04,708 --> 00:13:09,708 or if S equals E, 250 250 00:13:12,070 --> 00:13:13,763 then let's log to the console, 251 251 00:13:15,270 --> 00:13:18,065 you got the middle seat, 252 252 00:13:18,065 --> 00:13:20,960 which is the one that no one wants. 253 253 00:13:20,960 --> 00:13:23,923 Well, let's add an emoji here. 254 254 00:13:26,270 --> 00:13:27,200 Let's say this one 255 255 00:13:28,740 --> 00:13:29,590 or else 256 256 00:13:33,880 --> 00:13:36,633 you got lucky, 257 257 00:13:39,220 --> 00:13:40,370 just any emoji here, 258 258 00:13:40,370 --> 00:13:42,030 just to make it different 259 259 00:13:43,030 --> 00:13:45,030 and I think we are good now. 260 260 00:13:45,030 --> 00:13:45,913 Let's test it. 261 261 00:13:47,010 --> 00:13:50,950 And so indeed this here contains the B at the end. 262 262 00:13:50,950 --> 00:13:52,840 And so therefore it's a middle seat, 263 263 00:13:52,840 --> 00:13:55,020 the same for this one 264 264 00:13:55,020 --> 00:13:58,653 and this person with this seat got lucky. 265 265 00:13:59,490 --> 00:14:03,900 All right and that's how we extract parts of strings. 266 266 00:14:03,900 --> 00:14:06,800 And that's something really important to do. 267 267 00:14:06,800 --> 00:14:08,160 So maybe if you'd like, 268 268 00:14:08,160 --> 00:14:10,160 you can play around with this some more, 269 269 00:14:10,160 --> 00:14:12,740 maybe come up with your own example. 270 270 00:14:12,740 --> 00:14:14,490 Now before we move on here, 271 271 00:14:14,490 --> 00:14:16,950 let's just stop for a second 272 272 00:14:16,950 --> 00:14:20,730 and understand why all of this actually works. 273 273 00:14:20,730 --> 00:14:23,900 So we know that strings are just primitives. 274 274 00:14:23,900 --> 00:14:26,500 So why do they have methods? 275 275 00:14:26,500 --> 00:14:28,710 Shouldn't methods only be available 276 276 00:14:28,710 --> 00:14:31,214 on objects such as a race? 277 277 00:14:31,214 --> 00:14:33,840 Well that is actually true. 278 278 00:14:33,840 --> 00:14:37,010 However, JavaScript is really smart. 279 279 00:14:37,010 --> 00:14:39,190 And so here is how this works. 280 280 00:14:39,190 --> 00:14:41,960 Whenever we call a method on a string, 281 281 00:14:41,960 --> 00:14:45,230 JavaScript will automatically behind the scenes 282 282 00:14:45,230 --> 00:14:49,080 convert that string primitive to a string object 283 283 00:14:49,080 --> 00:14:50,940 with the same content. 284 284 00:14:50,940 --> 00:14:55,220 And then it's on that object where the methods are called. 285 285 00:14:55,220 --> 00:14:58,850 All right and this process is called boxing 286 286 00:14:58,850 --> 00:15:01,370 because it basically takes our string 287 287 00:15:01,370 --> 00:15:03,460 and puts it into a box 288 288 00:15:03,460 --> 00:15:05,510 which is the object. 289 289 00:15:05,510 --> 00:15:08,253 So basically what happens is this. 290 290 00:15:11,820 --> 00:15:14,163 So what JavaScript does, 291 291 00:15:15,190 --> 00:15:17,363 is to call this string function here. 292 292 00:15:20,980 --> 00:15:21,813 Okay. 293 293 00:15:21,813 --> 00:15:24,960 And so now you'll see, that this string here, 294 294 00:15:24,960 --> 00:15:27,860 looks a little bit more like an object. 295 295 00:15:27,860 --> 00:15:29,920 And we could take a look at this here. 296 296 00:15:29,920 --> 00:15:32,883 And so you see here, all of these methods, 297 297 00:15:33,840 --> 00:15:36,690 like slice that we just use. 298 298 00:15:36,690 --> 00:15:38,700 But this is beyond the scope of this lecture. 299 299 00:15:38,700 --> 00:15:42,210 We will learn what this is a little bit later. 300 300 00:15:42,210 --> 00:15:46,713 But what matters is that this indeed is now an object. 301 301 00:15:48,380 --> 00:15:50,580 So you see it is an object. 302 302 00:15:50,580 --> 00:15:52,400 And so this conversion here 303 303 00:15:52,400 --> 00:15:54,930 is what JavaScript does behind the scenes 304 304 00:15:54,930 --> 00:15:58,450 whenever we call a method on a string. 305 305 00:15:58,450 --> 00:16:01,020 And then when the operation is done 306 306 00:16:01,020 --> 00:16:05,200 the object is converted back to a regular string primitive. 307 307 00:16:05,200 --> 00:16:06,400 Okay. 308 308 00:16:06,400 --> 00:16:09,740 And in fact all string methods return primitives. 309 309 00:16:09,740 --> 00:16:12,733 Even if called on a string object. 310 310 00:16:13,640 --> 00:16:16,773 So let me demonstrate us here as well. 311 311 00:16:19,930 --> 00:16:21,163 Just really quick. 312 312 00:16:25,260 --> 00:16:27,113 And actually we want your type off. 313 313 00:16:29,760 --> 00:16:31,750 And so the result of all of this, 314 314 00:16:31,750 --> 00:16:33,683 is then back to being a string. 315 315 00:16:35,640 --> 00:16:37,623 Okay, but don't get confused by this. 316 316 00:16:37,623 --> 00:16:40,210 This is just a theory explanation 317 317 00:16:40,210 --> 00:16:44,030 behind why all of this works in case you're curious. 318 318 00:16:44,030 --> 00:16:45,870 And I hope you are. 319 319 00:16:45,870 --> 00:16:46,703 Great. 320 320 00:16:46,703 --> 00:16:48,520 But now let's move on to the next video, 321 321 00:16:48,520 --> 00:16:51,163 where we will continue working with strings. 26616

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