- Secondly, it processes your code line after lines, reading and parsing the data on the line.
-The first step describes the term "hoisting", but i would still like to explain further on this term, so i wouldnt stop reading just yet . There is more i would like you to get, to ensure you solidify your understanding of what "hoisting" means.
-Your code is not what is really given to the computer directly rather, it is translated and converted by the compiler to data that the computer system can understand and execute.
From this example, it can be seen that in the code, the function has been called from the top and the variable outputed from the top using the
Looking at the result in the console of the browser, the function has been called but the variable is 'undefined'. Why did only the function work?, does hoisting only work for functions?, you might ask. But the fact is that hoisting actually works for both of them and it did work! but it just didnt give what you'd expect. The reason for this is that
When your code was run, the execution context was created(that abstract enviroment where codes are managed and compiled) in two phases; storing the functions and variables, and then processing the code.
The function was stored in 'whole', meaning that the function itself(
myFunction) and the string data it held("
function has been called") were all stored in memory for use.
And so it didnt matter that it was called some lines before actually getting to the function itself because during the first phase of the execution context, it was already existing in memory.
But in the case of a variable, it is not the same story. This is because only the declaration of the variable(
var x) was setup and stored in memory, the initialization(
Another thing to understand is that there is a difference in results between declaring a variable without assigning anything to it and not declaring a variable at all when trying to output it on the console of your browser.
In this case, it can be seen in the code that the variable has been declared but without any value assigned to it.
And as you would deduce from the previous explanation, the results in the console of the browser was outputed as undefined because it doesnt matter when the code is executed from top to bottom if there is a value assigned or not as the initial value will be set to undefined except the initialization was set before outputing it('console.logging').
In this case, the variable hasnt been declared but is being outputed from the code. The results in the browser is as thus,
Now, from the console of the browser, we are getting an '
Uncaught ReferenceError' message saying x hasnt been defined(declared or initialized).
Ok but hol up now, what is the difference between the word 'Undefined' in the first case and this second case where there is an error showing that the variable x has not been defined. Now, there is something you need to understand about this two results.
This is my personal definition of it anyways and the way i understand it to be and hope you understand it the same way.
I hope this entry on what hoisting is has been helpful and efficient to your understanding. Thank you for reading and keep improving!!!
Follow me @ Twitter