- [Instructor] For some, JavaScript's code
can be a bit intimidating but it's very simple.
Put in its simplest terms, scope determines accessibility
or visibility of the variable.
Depending where and what type of scope we are using
it will determine if your variable is available
when you call it.
Let's type some code to visualize what scope is.
Let's remove the console log here
and let's add a warrior, so var warrior = Ninja.
Then let's create a function.
We'll create a function with a const this time,
and we'll call this one screamWarrior
and we won't take any parameters.
Inside of that function, we'll define another variable,
warrior2 = Samurai.
Then inside of the function, we'll console.log warrior
and warrior2 and outside of the function definition
we'll run the function and then we'll console.log
the warrior1 or the warrior and warrior2
and you can save this.
When you look into this code here,
you have the warrior and the warrior2 variables defined,
but because they are defined with different types,
but also in different places, their scope or accessibility
is different.
I'm also using var in this example and we'll explore
the differences between var, let and const in this course.
But if you've been following most
of my courses, and also using the latest JS syntax
you've been using let and const and var is rarely used.
Let and const are part of the block scope variables
and we're introducing ES6, it is
what most JS developers use nowadays.
But it's important to get a glimpse in the past as well,
as there is still lots of legacy code using var,
and you need to understand their nuances.
Related to scope, we'll also explore what closures are
and what is all these things, so you understand
what happens when variables are hoisted to the top
of your code.
So let's move on.