- [Narrator] Global variables are any functions,
objects, variables that are declared outside of functions.
And when we say global,
we mean they are accessible throughout your file.
And this is a good time to introduce
the variable, lifetime.
A term you may hear from time to time.
The variable lifetime is when the variable is deleted
after it has been declared.
In the case of global variables,
it is available when declared
and deleted when the application is closed,
in most case when the browser is closed.
And for that reason,
it is often not recommended to use global variables.
Let's say for example you initialize a global variable
with user data.
This can be a privacy or security issue.
As someone could literally pull data from this variable
in the browser.
Two nuances to global variables.
If you don't declare a variable and you assign a value
to a variable inside of a function,
it automatically becomes globally available.
Another danger,
it is always better to declare your variables.
But if you use strict mode in JavaScript,
they won't be automatically global.
Let's take a look at all this in code.
Let's create a new variable
that we'll call a warrior.
And this one we'll use a const,
and we'll create an object,
that will hold properties for this warrior.
So a name,
Jujin
Take.
Let's give him a type.
He's a ninja.
Let's give him a weapon.
And let's give him a shuriken.
And finally let's give him some agility.
And let's do 79.
And let's correct a little typo
we have here on the weapon.
Awesome.
Right now if we look at this guy here,
well this guy is globally scoped.
So it means that it is available all throughout this file,
or the global scope.
Now if we actually go to the function,
this function is also global in scope.
Because it is declared in the global scope here.
It's not declared inside of a function,
so this is global as well.
Now let's remove that comment here.
Let's define a variable without declaring it.
We'll do warrior three,
and put viking to it.
If you look at this,
this is the nuance of global scope.
If we declare a variable here,
if we do const, let, or var warrior two.
This is locally scoped.
But this is the nuance of global scope.
If we actually do not use let,
or const or var,
when we're declaring this variable inside,
it will automatically be globally scoped.
So be very very careful with that
and always declare your variables.
In this case, this one will be globally available.
And I'll show you in a second.
If we go to our console logs
in here,
and let's remove that code for now.
If you wanna take a look at that code,
and if you wanna keep it,
go back to the exercise files,
we have copies of that code.
If we remove this,
we can actually console log warrior,
and also warrior three.
Because warrior three is in here,
but it hasn't been declared
and therefore is automatically declared
as a global variable.
Let's save that,
and let's go back here.
And as you can see,
we have not only our object here with all the properties,
but we also have at the end a samuri
which is the local variable here.
And that's this function here.
But if we go back to the second one,
we shouldn't see viking.
Why is viking here?
Like I said, because this variable hasn't been declared
before it is initialized.
Then this is automatically declared as a global variable.
Now you could do something
that would change this automatically.
If you use strict in your JavaScript,
if you do this syntax,
use strict,
and you save.
Now this variable here will not be available globally.
If we save this,
we're gonna get an error into our code.
Warrior three is not defined.
Whatever you do,
declare your variables.
Always use let, const, or var.
And I would recommend to use let or const,
since this is the latest syntax with the S6.
But if you are starting to use or declare your variables
without let inside of a function,
then make sure you use strict so you catch these errors.
But I would always suggest to declare your variables
correctly with the little syntax const, let, or var first.
Let's move on.