Still, I liked some of the example code in this post because it can be used to explain some interesting things about JavaScript as a language. The following is an in-depth explanation of what is happening in each of those examples.
Example #1
This strange looking piece of code seems to say, “if window doesn’t have a property ‘a’, define a variable ‘a’ and assign it the value of 1.” You would then expect the alert to display the number 1. In reality, the alert displays “undefined”. To understand why this happens, you need to know three things about JavaScript.
First, all global variables are properties of
window
. Writing var a = 1
is functionally equivalent to writing window.a = 1
. You can check to see if a global variable is declared, therefore, by using the following:
Second, all variable declarations are hoisted to the top of the containing scope. Consider this simpler example:
The alert in this case outputs “true” even though the variable declaration comes after the test. This is because the JavaScript engine first scans for variable declarations and moves them to the top. The engine ends up executing the code like this:
Reading this code, it makes far more sense as to why the alert would display “true”.
The third thing you need to understand to make sense of this example is that while variabledeclarations are hoisted, variable initializations are not. This line is both a declaration and an initialization:
You can separate out the declaration and the initialization like this:
When the JavaScript engines comes across a combination of declaration and initialization, it does this split automatically so that the declaration can be hoisted. Why isn’t the initialization hoisted? Because that could affect the value of the variable during code execution and lead to unexpected results.
So, knowing these three aspects of JavaScript, re-examine the original code. The code actually gets executed as if it were the following:
Looking at this code should make the solution obvious. The variable
a
is declared first, and then the if
statement says, “if a
isn’t declared, then initialize a
to have a value of 1.” Of course, this condition can never be true and so the variable a remains with its default value,undefined
.Example #2
This code looks far more complex than it actually is. The result is that the alert displays the number 1, the value to which a was initialized. But why is that? Once again, this example relies on knowledge of three key aspects of JavaScript.
The first concept is that of variable declaration hoisting, which example #1 also relied upon. The second concept is that of function declaration hoisting. All function declarations are hoisted to the top of the containing scope along with variable declarations. Just to be clear, a function declaration looks like this:
This is opposed to a function expression, which is a variable assignment:
To be clear, function expressions are not hoisted. This should make sense to you now, as just with variable initialization, moving the assignment of a value from one spot in code to another can alter the execution significantly.