The following two examples will achieve the same result:
Example 1 gives indistinguishable outcome from Example 2:
To understand the above example, you need to understand "hoisting".
Variable promotion: Function declarations and variable declarations are always silently "promoted" to the top of the method body by the interpreter.
The following two examples have different results: p>
Does it bode well that y is unclear in the last example?
This is on the grounds that just the affirmation (var y), not the instatement (=7) is raised to the top.
Because of raising, y has been proclaimed before it is utilized, but since instatements are not raised, the estimation of y is undefined.
Example 2 is equivalent to writing:
var y; /Declare y
elem = document.getElementById("demo");/Find a component
elem.innerHTML = x + " " + y; //Display x and y
y = 7; /Assign 7 to y
Declare Your Variables At the Top !
If programmers don't understand hoisting well, the programs they write are prone to problems.
In the next chapter we will learn "strict mode" .