All language subtitles for 003 Adding JavaScript To A Page & How React Projects Differ_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
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 Download
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
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 00:00:02,070 --> 00:00:02,940 So let's start 2 00:00:02,940 --> 00:00:06,840 by revisiting certain crucial core concepts 3 00:00:06,840 --> 00:00:10,050 and things you should know about JavaScript, 4 00:00:10,050 --> 00:00:13,080 for example, that JavaScript can be executed 5 00:00:13,080 --> 00:00:15,060 in many different environments. 6 00:00:15,060 --> 00:00:17,370 For example, of course, the browser, 7 00:00:17,370 --> 00:00:19,770 that's where JavaScript originated. 8 00:00:19,770 --> 00:00:22,380 In the end, that's what it was built for 9 00:00:22,380 --> 00:00:24,000 back in the nineties. 10 00:00:24,000 --> 00:00:27,630 But nowadays, thanks to NodeJS and Deno, 11 00:00:27,630 --> 00:00:31,020 for example, you can also execute JavaScript code 12 00:00:31,020 --> 00:00:32,850 outside of the browser. 13 00:00:32,850 --> 00:00:35,370 This will not be the focus of this course 14 00:00:35,370 --> 00:00:37,320 because this is a React course 15 00:00:37,320 --> 00:00:39,960 and React is a client-side, front-end, 16 00:00:39,960 --> 00:00:42,840 browser-based library in its core 17 00:00:42,840 --> 00:00:45,930 that allows you to build user interfaces 18 00:00:45,930 --> 00:00:48,150 with React and JavaScript. 19 00:00:48,150 --> 00:00:49,890 But it is of course worth knowing 20 00:00:49,890 --> 00:00:53,580 that JavaScript is not limited to the browser. 21 00:00:53,580 --> 00:00:57,090 You can even build mobile apps with JavaScript, 22 00:00:57,090 --> 00:00:59,070 thanks to extra technologies 23 00:00:59,070 --> 00:01:01,740 like Capacitor or React Native, 24 00:01:01,740 --> 00:01:02,850 which by the way, 25 00:01:02,850 --> 00:01:04,560 would be a great next step 26 00:01:04,560 --> 00:01:06,660 after finishing this course. 27 00:01:06,660 --> 00:01:08,670 You can also use React 28 00:01:08,670 --> 00:01:11,100 in conjunction with another tool 29 00:01:11,100 --> 00:01:13,260 or another library you could say, 30 00:01:13,260 --> 00:01:14,880 called React Native, 31 00:01:14,880 --> 00:01:17,070 to build native mobile apps 32 00:01:17,070 --> 00:01:19,260 for Android and iOS. 33 00:01:19,260 --> 00:01:20,100 And I do have 34 00:01:20,100 --> 00:01:22,410 a dedicated course on React Native 35 00:01:22,410 --> 00:01:23,940 if you wanna learn more about that 36 00:01:23,940 --> 00:01:25,800 after finishing this course. 37 00:01:25,800 --> 00:01:29,640 So JavaScript is really versatile, really powerful. 38 00:01:29,640 --> 00:01:31,740 But in this refresher section, 39 00:01:31,740 --> 00:01:33,000 we are going to focus 40 00:01:33,000 --> 00:01:35,100 on JavaScript in the browser, 41 00:01:35,100 --> 00:01:36,870 though the general syntax 42 00:01:36,870 --> 00:01:38,160 and rules of course, 43 00:01:38,160 --> 00:01:39,750 are always the same, 44 00:01:39,750 --> 00:01:41,760 no matter for which environment 45 00:01:41,760 --> 00:01:44,310 you are writing your JavaScript code. 46 00:01:44,310 --> 00:01:47,430 So since we focus on JavaScript in the browser, 47 00:01:47,430 --> 00:01:49,110 let's explore how JavaScript code 48 00:01:49,110 --> 00:01:51,270 can be added to a website. 49 00:01:51,270 --> 00:01:54,360 And here you have two main options 50 00:01:54,360 --> 00:01:57,840 and they both include the special script tag, 51 00:01:57,840 --> 00:01:59,430 the special script element, 52 00:01:59,430 --> 00:02:02,400 which you can add to your HTML code. 53 00:02:02,400 --> 00:02:04,560 You can either put your JavaScript code 54 00:02:04,560 --> 00:02:06,540 between such script tags, 55 00:02:06,540 --> 00:02:08,910 or you can use those script tags 56 00:02:08,910 --> 00:02:11,370 to import JavaScript files, 57 00:02:11,370 --> 00:02:14,370 which then contain your JavaScript code. 58 00:02:14,370 --> 00:02:15,203 Now typically, 59 00:02:15,203 --> 00:02:17,910 we don't use that inline approach 60 00:02:17,910 --> 00:02:19,020 on the left here, 61 00:02:19,020 --> 00:02:22,170 where the code is between those script tags, 62 00:02:22,170 --> 00:02:23,550 because this approach 63 00:02:23,550 --> 00:02:25,680 can quickly lead to unmaintainable 64 00:02:25,680 --> 00:02:27,900 and complex HTML files, 65 00:02:27,900 --> 00:02:29,280 and is therefore typically 66 00:02:29,280 --> 00:02:32,640 only used for very short scripts. 67 00:02:32,640 --> 00:02:33,720 Instead, typically, 68 00:02:33,720 --> 00:02:35,370 you have your JavaScript code 69 00:02:35,370 --> 00:02:37,980 in dedicated JavaScript files 70 00:02:37,980 --> 00:02:40,950 because such projects that use this approach 71 00:02:40,950 --> 00:02:43,800 are way easier to maintain. 72 00:02:43,800 --> 00:02:45,960 Therefore, here in this demo project 73 00:02:45,960 --> 00:02:47,130 which I prepared, 74 00:02:47,130 --> 00:02:49,410 we could start in this assets folder 75 00:02:49,410 --> 00:02:51,960 by adding a new sub-folder 76 00:02:51,960 --> 00:02:53,520 that could be called scripts, 77 00:02:53,520 --> 00:02:55,920 though the name of course doesn't matter. 78 00:02:55,920 --> 00:02:56,753 And in there, 79 00:02:56,753 --> 00:02:58,860 we can create a JavaScript code file 80 00:02:58,860 --> 00:03:02,400 that could be named app.js. 81 00:03:02,400 --> 00:03:04,860 Though this name also is totally up to you, 82 00:03:04,860 --> 00:03:07,440 it just should end with .js, 83 00:03:07,440 --> 00:03:09,570 so instead of course tells the browser 84 00:03:09,570 --> 00:03:11,340 and also the Code Editor 85 00:03:11,340 --> 00:03:14,253 that this file will contain JavaScript code. 86 00:03:15,210 --> 00:03:17,070 And the Code Editor can therefore, 87 00:03:17,070 --> 00:03:19,350 for example, make sure that the code 88 00:03:19,350 --> 00:03:22,170 is properly highlighted and formatted. 89 00:03:22,170 --> 00:03:24,300 Well, and with such a file added here, 90 00:03:24,300 --> 00:03:27,960 you can then import it in the HTML file 91 00:03:27,960 --> 00:03:29,520 by going there 92 00:03:29,520 --> 00:03:32,100 and adding your script tags, 93 00:03:32,100 --> 00:03:34,680 opening and closing script tags. 94 00:03:34,680 --> 00:03:36,960 And you always need both. 95 00:03:36,960 --> 00:03:39,840 You can't use a self-closing script tag 96 00:03:39,840 --> 00:03:41,070 or anything like this. 97 00:03:41,070 --> 00:03:42,540 Instead, you need opening 98 00:03:42,540 --> 00:03:44,100 and closing script tags. 99 00:03:44,100 --> 00:03:45,723 That's a must have. 100 00:03:46,620 --> 00:03:47,730 Now you can add them 101 00:03:47,730 --> 00:03:49,950 anywhere you want in that file. 102 00:03:49,950 --> 00:03:51,510 In the head section, 103 00:03:51,510 --> 00:03:52,980 in the body section, 104 00:03:52,980 --> 00:03:55,470 really, anywhere you want. 105 00:03:55,470 --> 00:03:58,170 And then you would add the source attribute, 106 00:03:58,170 --> 00:03:59,970 the src attribute 107 00:03:59,970 --> 00:04:01,710 to point at the file. 108 00:04:01,710 --> 00:04:03,870 In my case, in the assets folder. 109 00:04:03,870 --> 00:04:05,580 And there in the scripts folder 110 00:04:05,580 --> 00:04:07,263 that should be imported. 111 00:04:08,430 --> 00:04:10,230 And now that file would be loaded 112 00:04:10,230 --> 00:04:11,670 as part of this webpage 113 00:04:11,670 --> 00:04:13,890 when the webpage is loaded. 114 00:04:13,890 --> 00:04:15,300 Now it is worth noting that 115 00:04:15,300 --> 00:04:18,029 you can then add extra attributes 116 00:04:18,029 --> 00:04:19,230 to the script tag. 117 00:04:19,230 --> 00:04:21,180 For example, the defer attribute, 118 00:04:21,180 --> 00:04:23,370 to make sure that the script 119 00:04:23,370 --> 00:04:25,080 that will be imported, 120 00:04:25,080 --> 00:04:27,023 will only be executed 121 00:04:27,023 --> 00:04:29,520 after the rest of the HTML document 122 00:04:29,520 --> 00:04:31,920 has been read and parsed. 123 00:04:31,920 --> 00:04:33,360 This ensures that, 124 00:04:33,360 --> 00:04:34,830 if your script code, 125 00:04:34,830 --> 00:04:36,780 for example, needs to work 126 00:04:36,780 --> 00:04:39,270 with some HTML elements, 127 00:04:39,270 --> 00:04:41,850 those HTML elements are available 128 00:04:41,850 --> 00:04:44,910 when the JavaScript code starts executing. 129 00:04:44,910 --> 00:04:45,990 Without defer, 130 00:04:45,990 --> 00:04:48,210 it would start executing right away. 131 00:04:48,210 --> 00:04:49,590 And if you then would try 132 00:04:49,590 --> 00:04:51,690 to reach out to this unordered list, 133 00:04:51,690 --> 00:04:54,060 for example, this unordered list 134 00:04:54,060 --> 00:04:55,830 might not be available yet 135 00:04:55,830 --> 00:04:57,180 if the script is loaded 136 00:04:57,180 --> 00:05:00,000 before the list is output here. 137 00:05:00,000 --> 00:05:02,340 That's why you might want to add defer. 138 00:05:02,340 --> 00:05:04,650 Now in modern JavaScript projects, 139 00:05:04,650 --> 00:05:06,510 it's also quite common 140 00:05:06,510 --> 00:05:08,460 that you have another attribute 141 00:05:08,460 --> 00:05:09,480 on your script tags 142 00:05:09,480 --> 00:05:11,310 instead of having defer. 143 00:05:11,310 --> 00:05:13,620 And that would be the type attribute, 144 00:05:13,620 --> 00:05:17,133 which then is set to module like this. 145 00:05:18,630 --> 00:05:21,510 Now, this type module attribute 146 00:05:21,510 --> 00:05:23,820 here on the script tag makes sure 147 00:05:23,820 --> 00:05:25,740 that this JavaScript file 148 00:05:25,740 --> 00:05:28,380 is treated as a JavaScript module. 149 00:05:28,380 --> 00:05:29,610 And if you are treating 150 00:05:29,610 --> 00:05:32,040 your JavaScript files as modules, 151 00:05:32,040 --> 00:05:35,130 this unlocks one very important new syntax. 152 00:05:35,130 --> 00:05:37,320 The import syntax, 153 00:05:37,320 --> 00:05:39,000 where you can import code 154 00:05:39,000 --> 00:05:41,973 from script A into script B. 155 00:05:42,930 --> 00:05:44,370 For example, here we could have 156 00:05:44,370 --> 00:05:45,933 a second script file, 157 00:05:47,400 --> 00:05:48,900 util.js, 158 00:05:48,900 --> 00:05:51,570 which might contain some utility functionality. 159 00:05:51,570 --> 00:05:52,980 And in that script file, 160 00:05:52,980 --> 00:05:54,480 we could export something 161 00:05:54,480 --> 00:05:56,160 with the export keyword, 162 00:05:56,160 --> 00:05:57,300 and then import it 163 00:05:57,300 --> 00:05:58,710 in the app.js file. 164 00:05:58,710 --> 00:06:02,100 Now we'll explore this import export syntax 165 00:06:02,100 --> 00:06:04,620 and this general module feature 166 00:06:04,620 --> 00:06:06,420 in greater detail soon. 167 00:06:06,420 --> 00:06:08,280 But before we get there, 168 00:06:08,280 --> 00:06:10,320 there's one other super important thing 169 00:06:10,320 --> 00:06:12,870 you should know about the script tag 170 00:06:12,870 --> 00:06:14,760 here in the HTML file. 171 00:06:14,760 --> 00:06:16,590 Because before we get there, 172 00:06:16,590 --> 00:06:19,170 there is one other super important thing 173 00:06:19,170 --> 00:06:21,900 you should know about these script tags 174 00:06:21,900 --> 00:06:24,360 and about importing script files 175 00:06:24,360 --> 00:06:27,240 in the context of building React apps. 176 00:06:27,240 --> 00:06:28,470 Because when you're working 177 00:06:28,470 --> 00:06:30,060 on a React project, 178 00:06:30,060 --> 00:06:33,930 you will almost never add these script tags 179 00:06:33,930 --> 00:06:36,360 to your HTML file on your own, 180 00:06:36,360 --> 00:06:38,850 because React projects almost 181 00:06:38,850 --> 00:06:41,940 always use a build process 182 00:06:41,940 --> 00:06:44,880 which as part of that build process, 183 00:06:44,880 --> 00:06:47,190 injects these script tags 184 00:06:47,190 --> 00:06:49,440 into the HTML code for you. 185 00:06:49,440 --> 00:06:51,390 And then the next lecture will explore 186 00:06:51,390 --> 00:06:53,850 why we use such a build process 187 00:06:53,850 --> 00:06:55,353 in React projects. 12934

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