February 8th, 2021
This is an unwanted behaviour because it can often lead some unpredictable and hard to find bugs. Better practice would be limiting the availability of your variables via using scopes. Now let's take a look at how can we use the scopes.
When you define a variable inside of a function your function will be automatically belong to that function's scope from that moment on. Let's check that out on examples.
As we can see above while we can reach out "one, two, three" inside of the "scoped" function we can not do the same outside of it.
As we can see above when "innerNest" function can reach "outer" variable, "outerNest" function can't reach the "inner" variable.
As we can see above while we can easily reach out the "insideOfIfVar" variable outside of the "if check" we defined, we can't do the same for the others.
Everything that are declared inside of a ".js" file are belongs to that specific file if they are not explicitly exported out. When you try to reach out a variable outside of its module without exporting it the chances are you are going to see something like this one.
Alright everyone, that was it for today. I hope you enjoyed it. Until next time, take care :)