What is Scoping?
The function ‘bar’ has it’s own scope and anything that will be defined in the function could not be accessed outside of the function.
The global scope can be accessed from everywhere, as a rule of thumb there should be as little global scope variable as possible. A lot of global variables can cause conflicts in complex applications.
Access from everywhere means that you can access the global scope from any portion of your code, at any time. When a variable is scoped globally it can also be accessed through the ‘window’ object, I mention that because of the next example:
There are mainly 3 ways to declare a variable in the global scope:
Each variable that will be declared in the main script, that means not inside a function will be scoped in the global scope
Declaring a variable without using the ‘var’ keyword will cause the variable to be scoped globally, no matter where it was declared. This is a huge pitfall for a lot of beginners.
Adding a property directly to the ‘window’ object - window.foo = “hello” (like the example above)
From the above example we can learn that although the scoped var foo in the function is now “hello” the original foo variable is still accessible through the window object who holds all the global variables.
As I said before, any variable or function that is declared outside a function or object will be scoped automatically in the global scope.
Always Declare your Variables
When declaring a variable – it should always be prepended with “var”, not doing this will cause the variable to be scoped globally. that can cause an unexpected behavior.
Look at the next example in order to understand how forgetting to prepend a variable with the ‘var’ keyword will cause the ‘bar’ variable to be scoped globally :
To understand the problem that could arise from this, take a look at the next example:
The ‘var’ keyword was not used in the for loop. Variable ‘i’ is used in the global scope. The console will not say that “i is true” because ‘i’ is actually equals to zero. we called the foo() function before our if.
So to summarize how you should use any var in a function, don’t use it in a global scope like that:
You might think it is better to do it like this:
But you will be wrong, well, at least not 100% right.. The best way is to declare it at the beginning of the scope:
Why? well, it is the preferred way because of what is called Hoisting.
Hoisting is a strange behavior when you first hearing about it, what it means is that any variables / function that belong to the current scope will be declared at the beginning of the scope. the difference between a variable and a function hoisting is that the function will have a value in the beginning of the scope while a variable will be undefined.
Why is that something that is important to know? take a look at the next example:
you would think that the output will first log ‘hello’ and ‘world’ but it will actually will log ‘undefined’ and ‘world’. why? Hoisting! that is why… with hoisting your code will actually be interpreted as:
So in order to not be surprised with unexpected behavior – it will be a good idea to always declare any variable that is used in the scope at the beginning of the scope.
Unlike variables, functions have their value when called before their declaration. You can see that from the last example where we called the ‘bar’ function before it was declared, That was possible because of hoisting that was interpreted into: