So to finish up here I have a quick little quiz.
I'm just going to paste in some code and I'd like you to evaluate mentally without typing this up what
you think this returns.
So we define the function and then recalling it at the end.
So what's returned from do math
.
So the answer here is true.
So let's step through how that works.
Very first thing we're declaring numb to be 8.
Then we have a function do math but that doesn't run yet.
So the next thing that runs is numb plus equals 1.
So number is now 9.
So this is the same as this one appear because they're in the same scope then we're calling and do math
.
So instead of do math we're writing numb plus equals 1.
SO NUMB was nine.
This is referring to the same number.
So it's now 10.
So all this does here is check if numb is evenly divisible by five.
So 10 is divisible by five.
So we return true and that's it.
So as I mentioned scope is notoriously tricky to explain.
It's something that we've only scratched the surface of here but we covered the important parts which
is that anytime you define a new function it gets its own new scope and it can access variables that
were defined outside the function but inside a function if we defined something it's not accessible
outside the function.
So there's one more quick example I want to highlight which is if I have two functions one called the
high end inside of hi I'm just going to declare a variable name equals Rusti and then I'm going to cost
about Lague name
and then I'm going to have another function called by and I'm not going to define Rustie there but I
am going to Conchobar blog name if I run high and I execute it.
I of course get rusty because I defined a variable name is Rusty instead of high.
And then I printed it.
If I do this with by However I get undefined
and that's happening because name is not accessible in the scope of this by function.
So this goes to show that every function has its own scope and they're not shared between functions
.