Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
Now before continuing to learn JavaScript, 2
2
in this new section, 3
3
we should first activate a special mode in JavaScript, 4
4
called Strict Mode. 5
5
And here on my desktop I already have the folder, 6
6
for this part 2 fundamental section. 7
7
And for you as I explained before, 8
8
these starred files come from the get up repertory 9
9
that I showed you at the beginning of the course. 10
10
So use that get up reparatory as your starter folder 11
11
or ofcourse you can just copy that data 12
12
somewhere else to your computer and work from there, 13
13
whatever works for you. 14
14
Now I'm creating here in UVS code window 15
15
and lets open this new folder. 16
16
So that's our new working folder 17
17
and this time we already have the script file here 18
18
and in the HDML file, 19
19
we see that the script is also already linked, 20
20
so we don't have to do that this time. 21
21
At this point I assume that you already know how to do that. 22
22
So closing the side bar here 23
23
and lets now get started in this new section. 24
24
And as I said lets start with strict mode. 25
25
So strict mode is a special mode 26
26
that we can activate in JavaScript, 27
27
which makes it easier for us 28
28
to write a secure JavaScript code. 29
29
And all we have to do to activate strict mode 30
30
is to write this ring at the beginning of the script 31
31
so use strict and that's it. 32
32
So with this, we activated strict mode 33
33
for the entire script. 34
34
Now what's important is that this line of code, 35
35
so this statement here basically 36
36
has to be the very first statement in the script. 37
37
So if we have any code before this 38
38
then strict mode will not be activated. 39
39
Comments are allowed because JavaScript 40
40
will just ignore them but no code. 41
41
Okay, now we actually can also activate strict mode, 42
42
only for a specific function or a specific block. 43
43
But I don't really see the point in doing that 44
44
and so I always just use it at the beginning of each script 45
45
and so I believe that you should do the same. 46
46
So always just put strict mode 47
47
in the beginning of your scripts 48
48
and like that write more secure code. 49
49
And when secure, I mean that strict mode 50
50
makes it easier for us developers 51
51
to avoid accidental errors. 52
52
So basically it helps us introduce the bugs into our code 53
53
and that's because of 2 reasons. 54
54
First, strict mode forbids us to do certain things 55
55
and second, it will actually create visible errors for us 56
56
in certain situations in which without strict mode 57
57
JavaScript will simply fail silently 58
58
without letting us know that we did a mistake. 59
59
Okay. 60
60
So again, first strict mode forbids us to do certain things 61
61
and second it creates visible errors 62
62
and the developer console, where in other situations 63
63
JavaScript would just fail silently. 64
64
Okay, but now to make this a bit more clear, 65
65
Lets actually see an example of 66
66
one of the most important changes 67
67
that strict mode introduces. 68
68
So lets create a variable here 69
69
with a complicated name lets say hasDriverslicense 70
70
and lets initialize it as false here. 71
71
And then I'll also create, pass test and set that to true. 72
72
And I'm using let in both of them 73
73
because I want to change them later 74
74
and actually this one not. 75
75
So lets just use a const here, 76
76
because remember by default we should always const 77
77
to avoid accidental errors. 78
78
So basically there is a certain person 79
79
which right now does not have a drivers license 80
80
but they just passed a test, 81
81
that's why we have passed test set to true. 82
82
And now we want to write some logic 83
83
so that when they pass the test 84
84
this variable here gets set to true. 85
85
So, we can say if passTest, right? 86
86
Because this one here is a boolean value 87
87
and so this condition will be either true or false. 88
88
And then lets introduce a simple bug here, okay? 89
89
So this is a kind of hard to write variable name 90
90
and so lets pretend that we are writing it wrong here. 91
91
So hasDriversLicense equal to true. 92
92
So what I did here was to basically omit the 's' 93
93
So this letter here is not in this variable 94
94
that am trying to change here to true. 95
95
And again that is an error I'm making here on purpose, 96
96
just to show you how strict mode can help us 97
97
avoid a certain kind of error, 98
98
because doing this is actually more common 99
99
than you might think, okay? 100
100
And now lets lock something to the console 101
101
if the person has the drivers license. 102
102
And so now were actually gonna go back 103
103
to writing it correctly. 104
104
So if hasDriversLicense and so you see that now 105
105
this one is exactly the same, 106
106
as VS code highlights both. 107
107
So if hasDriversLicense then log to the console, 108
108
I can drive. 109
109
And now lets actually start 110
110
by deactivating strict mode again, 111
111
just so we see how this bug could affect our code. 112
112
So I'm giving it a save and then lets go 113
113
and open this index.hdml file. 114
114
Okay? 115
115
Now lets open up to console here 116
116
and again I'm gonna increase the text 117
117
by hitting command plus a couple of times here. 118
118
Okay? 119
119
So lets reload here just to make sure 120
120
that everything is working 121
121
and now what we see is that it didn't log I can drive 122
122
to the console. 123
123
Even though we had attempted to set 124
124
hasDriversLicense to true here, 125
125
so we expected that drivers license being true 126
126
should then log I can drive. 127
127
But it didn't work and that's because of the mistake 128
128
that I just introduced here on purpose 129
129
forgetting this 's' 130
130
so without the error it should work, right? 131
131
But without it, its not gonna work. 132
132
And again because basically here 133
133
I set a new variable to true 134
134
and not the original one, 135
135
so this one is still false 136
136
and that's why here, this logging does not happen. 137
137
So now our program has a bug 138
138
which means that it has a mistake 139
139
and we would now go ahead and find that bug. 140
140
But, strict mode can actually help us with this. 141
141
So lets turn it back on, 142
142
give it a save, now try it again. 143
143
And now we actually get an error. 144
144
So here we see that hasDriversLicense is not defined, 145
145
so in line number 6 which is right here 146
146
and so this error message now tells us 147
147
exactly what is wrong. 148
148
So it tells us that this variable is not defined 149
149
and then we can see, oh we did a mistake here, 150
150
we're missing the 's', 151
151
right, so now its the same variable, 152
152
here, here and here, okay? 153
153
And so if we try that now again. 154
154
then the error is gone. 155
155
And so strict mode really helped us avoid this kind of bug 156
156
that we created here. 157
157
You can experiment a little bit with this 158
158
to make it really clear. 159
159
Now another thing that strict mode does 160
160
is to introduce a short list of variable names 161
161
that are reserved for features 162
162
that might be added to the language a bit later. 163
163
For example, lets try to define a variable called interface, 164
164
for example, for an audio interface 165
165
and now if we try this. 166
166
Then we see that, we get an unexpected strict mode 167
167
reserved word, error message basically 168
168
and that is again because JavaScript 169
169
is reserving this word here for a feature 170
170
that it might implement in the future. 171
171
And so by reserving these kind of words 172
172
it will then make it easier 173
173
to implement the feature in the future. 174
174
So another one is private for example, 175
175
so const private, just some number here 176
176
and so this one will also not work. 177
177
And that's because in the future 178
178
there might be something called a private field and classes 179
179
or in some other places even in the language 180
180
and they might want to use this word for that. 181
181
And so strict mode reserves these words 182
182
so we cannot use them for variables. 183
183
It's the same logic as this one 184
184
so we cannot call a variable 'if'. 185
185
Right? 186
186
And that's because there's already an if statement. 187
187
So here we get an unexpected token 'if' 188
188
so that's like the rules 189
189
that I explained to you in the beginning 190
190
about naming variables. 191
191
Okay? Alright. 192
192
Now there are actually a lot of other changes 193
193
that affect things that we haven't learned about yet. 194
194
So, that's stuff like functions, objects, 195
195
setting properties on primitive values and many more. 196
196
And I will mention them in future lectures 197
197
when the time comes. 198
198
But everything that I'm gonna say 199
199
in the rest of the course from now on 200
200
we'll always assume that you have strict mode turned on. 201
201
And so I will add this line of code here 202
202
to all the scripts in the future 203
203
and so you should do the same. 204
204
Anyway with this out of the way 205
205
lets now start learning functions.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.