Jake : Noice ! It would be “Hello world” and then “undefined”
Raymond Holt : Could you explain it in detail how this happens behind the scenes ?
Jake : Okay Cool ,cool , cool , cool , cool , cool
Raymond Holt : Are you nervous , Peralta ?
Jake : No , captain, I am a story teller . It’s my craft ! Here I come up with the best explanation.
Behind the scenes , there is this container called — “The execution context”. It consists of two components —
Raymond Holt : Give me the technical terms for these components Peralta !
Jake : Ya captain!
Raymond Hold : It’s Yes Peralta . Do not use “Ya” . Continue !
Jake : Yes captain !The memory component is nothing but the variable environment, and the code component is nothing but the thread of execution.
All the variables and functions are reserved space in the memory even before the code is executed.
In the case of variables , they are stored in the memory as a value undefined. “undefined” acts as a placeholder here. Whereas in the case of functions ,the entire copy of the function is stored in the memory.
In the second example, the memory has
already present in the memory .
So in the thread of execution i.e. the code component,
Also , if I use an arrow function like ,
It will print throw an error stating “Uncaught reference” as the function here is stored as a variable which is undefined.
Raymond Holt : Correct
Jake : Yeah ! I have said multiple correct things consecutively ! Noiceeee
Raymond Holt: Please refrain from using those words Peralta. One last question and we can end the discussion. What is undefined ? Is it same as not defined ?
Jake : Smort question ! No , they are not the same at all ! Undefined acts as a placeholder for a variable that has been declared in the code somewhere. Not defined indicates that there is no mention of the variable in the code anywhere.
Here the code throws an error “Uncaught ReferenceError: x is not defined “
Raymond Holt : Clear ! Nice explanation Peralta !
Jake : Thanks dad ! Umm Thanks captain !
Raymond Holt : I will be assigning you a project with Santiago , I hope you don’t mind!
Jake : Noiceee !
Not a doctor ssh , Fremulon
More about hoisting —
Connect with me on LinkedIn : Divya Rao | LinkedIn
Follow me on Twitter : Divya Rao (@dailydevDivya) / Twitter