JavaScript executes in a single-threaded apartment, just like GUI applications.
That means that multiple concurrent callers may request invocations of JavaScript functions by
enqueuing call contexts in an execution stack, but only a single thread deques each context and
invokes it until the input queue is empty.
The execution process is illustrated in this
diagram.
The JavaScript environment provides
an execution stack and a queue of event handlers. To begin execution of a JavaScript function, it's context is loaded
into the execution stack and begins running. If the running function calls another function the called function is
pushed onto the execution stack and begins running. When completed, each function context is popped from the stack,
and execution continues with the previous calling context, if any.
When the last function context is popped from the stack, any event handler context at the front of the event queue
is loaded into the execution stack and begins to run.