Instructor: Now before continuing to learn JavaScript,
in this new section,
we should first activate a special mode in JavaScript,
called Strict Mode.
And here on my desktop I already have the folder,
for this part 2 fundamental section.
And for you as I explained before,
these starred files come from the get up repertory
that I showed you at the beginning of the course.
So use that get up reparatory as your starter folder
or ofcourse you can just copy that data
somewhere else to your computer and work from there,
whatever works for you.
Now I'm creating here in UVS code window
and lets open this new folder.
So that's our new working folder
and this time we already have the script file here
and in the HDML file,
we see that the script is also already linked,
so we don't have to do that this time.
At this point I assume that you already know how to do that.
So closing the side bar here
and lets now get started in this new section.
And as I said lets start with strict mode.
So strict mode is a special mode
that we can activate in JavaScript,
which makes it easier for us
to write a secure JavaScript code.
And all we have to do to activate strict mode
is to write this ring at the beginning of the script
so use strict and that's it.
So with this, we activated strict mode
for the entire script.
Now what's important is that this line of code,
so this statement here basically
has to be the very first statement in the script.
So if we have any code before this
then strict mode will not be activated.
Comments are allowed because JavaScript
will just ignore them but no code.
Okay, now we actually can also activate strict mode,
only for a specific function or a specific block.
But I don't really see the point in doing that
and so I always just use it at the beginning of each script
and so I believe that you should do the same.
So always just put strict mode
in the beginning of your scripts
and like that write more secure code.
And when secure, I mean that strict mode
makes it easier for us developers
to avoid accidental errors.
So basically it helps us introduce the bugs into our code
and that's because of 2 reasons.
First, strict mode forbids us to do certain things
and second, it will actually create visible errors for us
in certain situations in which without strict mode
JavaScript will simply fail silently
without letting us know that we did a mistake.
Okay.
So again, first strict mode forbids us to do certain things
and second it creates visible errors
and the developer console, where in other situations
JavaScript would just fail silently.
Okay, but now to make this a bit more clear,
Lets actually see an example of
one of the most important changes
that strict mode introduces.
So lets create a variable here
with a complicated name lets say hasDriverslicense
and lets initialize it as false here.
And then I'll also create, pass test and set that to true.
And I'm using let in both of them
because I want to change them later
and actually this one not.
So lets just use a const here,
because remember by default we should always const
to avoid accidental errors.
So basically there is a certain person
which right now does not have a drivers license
but they just passed a test,
that's why we have passed test set to true.
And now we want to write some logic
so that when they pass the test
this variable here gets set to true.
So, we can say if passTest, right?
Because this one here is a boolean value
and so this condition will be either true or false.
And then lets introduce a simple bug here, okay?
So this is a kind of hard to write variable name
and so lets pretend that we are writing it wrong here.
So hasDriversLicense equal to true.
So what I did here was to basically omit the 's'
So this letter here is not in this variable
that am trying to change here to true.
And again that is an error I'm making here on purpose,
just to show you how strict mode can help us
avoid a certain kind of error,
because doing this is actually more common
than you might think, okay?
And now lets lock something to the console
if the person has the drivers license.
And so now were actually gonna go back
to writing it correctly.
So if hasDriversLicense and so you see that now
this one is exactly the same,
as VS code highlights both.
So if hasDriversLicense then log to the console,
I can drive.
And now lets actually start
by deactivating strict mode again,
just so we see how this bug could affect our code.
So I'm giving it a save and then lets go
and open this index.hdml file.
Okay?
Now lets open up to console here
and again I'm gonna increase the text
by hitting command plus a couple of times here.
Okay?
So lets reload here just to make sure
that everything is working
and now what we see is that it didn't log I can drive
to the console.
Even though we had attempted to set
hasDriversLicense to true here,
so we expected that drivers license being true
should then log I can drive.
But it didn't work and that's because of the mistake
that I just introduced here on purpose
forgetting this 's'
so without the error it should work, right?
But without it, its not gonna work.
And again because basically here
I set a new variable to true
and not the original one,
so this one is still false
and that's why here, this logging does not happen.
So now our program has a bug
which means that it has a mistake
and we would now go ahead and find that bug.
But, strict mode can actually help us with this.
So lets turn it back on,
give it a save, now try it again.
And now we actually get an error.
So here we see that hasDriversLicense is not defined,
so in line number 6 which is right here
and so this error message now tells us
exactly what is wrong.
So it tells us that this variable is not defined
and then we can see, oh we did a mistake here,
we're missing the 's',
right, so now its the same variable,
here, here and here, okay?
And so if we try that now again.
then the error is gone.
And so strict mode really helped us avoid this kind of bug
that we created here.
You can experiment a little bit with this
to make it really clear.
Now another thing that strict mode does
is to introduce a short list of variable names
that are reserved for features
that might be added to the language a bit later.
For example, lets try to define a variable called interface,
for example, for an audio interface
and now if we try this.
Then we see that, we get an unexpected strict mode
reserved word, error message basically
and that is again because JavaScript
is reserving this word here for a feature
that it might implement in the future.
And so by reserving these kind of words
it will then make it easier
to implement the feature in the future.
So another one is private for example,
so const private, just some number here
and so this one will also not work.
And that's because in the future
there might be something called a private field and classes
or in some other places even in the language
and they might want to use this word for that.
And so strict mode reserves these words
so we cannot use them for variables.
It's the same logic as this one
so we cannot call a variable 'if'.
Right?
And that's because there's already an if statement.
So here we get an unexpected token 'if'
so that's like the rules
that I explained to you in the beginning
about naming variables.
Okay? Alright.
Now there are actually a lot of other changes
that affect things that we haven't learned about yet.
So, that's stuff like functions, objects,
setting properties on primitive values and many more.
And I will mention them in future lectures
when the time comes.
But everything that I'm gonna say
in the rest of the course from now on
we'll always assume that you have strict mode turned on.
And so I will add this line of code here
to all the scripts in the future
and so you should do the same.
Anyway with this out of the way
lets now start learning functions.