As far as I know, there details are part of computer system architecture and you will be able to deal with these stuff if you were using C++ ( there may be other ways of doing it that I might not be aware of ). I will draw it out for you if you want.

Now as you can see, the stack grows downwards while the heap grows towards the top. Heap is dynamic memory and access to the heap is done using pointers in C++, hence a bit slow, but as it is accessed using pointers hence it can be accessed globally. This is the reason, global variable are stored in heap.
While on the other hand, local variables are stored in stack. Stack size is pretty limited and is usually done for variables used within a function and pretty much anything related to function calls and the more trivial stuff make use of the stack. That is why if you do a never ending recursion you will get a stack overflow.
Using the analogy I guess JS would use something of this sort under the hood. But the raw stack and heap thing can be visualised either by C / C++ or by assembly code.
How do I know this? Well I did a course on OS, so we had to do some coding that dealt with stack and heap. Pretty boring stuff.
Edit: The source code sits in the stack as well. And the mid point of the two, ie: stack and heap, that does not change, that is a static index.