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 —
- Memory component
- Code component
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.
As JavaScript is a synchronous single-threaded language , it executes each code line by line in the code component.
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