Is the global execution context (global EC) always in the execution context stack (ECS) while JS is running?

the most common saying is that the global execution context executes at the bottom of the context stack until the page closes.

however, in the event loop, there is another saying that js checks the task queue when the context stack is empty.

how can ECS be empty if the global execution context is in ECS all the time. Or is it that the two execution context stacks are not the same?


answer your own question:
there is only one execution context stack;
the global context is not always in the execution context stack.

that is, the global execution context we often hear stays at the bottom of the stack until the browser is closed error

then let me explain:
1, the global context goes off the stack and is not always at the bottom of the stack.
when all the code runs, the ECS is empty, that is, the global execution context goes off the stack, but the global lexical environment (global lexical environment) still exists. The global context is created again when
2, executes the global code.
when you execute global code (for example, from the console), the browser uses the global lexical environment to create a global context (of course, it's not the only one in the global context). The code is then executed in this global context. The context is created again when events in the queue are executed in the
3, event loop.
when ECS is empty, the browser removes a task from the task queue and creates an execution context, that is, the global context, with the information about the task.
for this, please refer to MDN :

.
At some point during the event loop, the runtime starts handling the messages on the queue, starting with the oldest one. To do so, the message is removed from the queue and its corresponding function is called with the message as an input parameter. As always, calling a function creates a new stack frame for that function's use.

and ES2015 specification :

A request for the future execution of a Job is made by enqueueing, on a Job Queue, a PendingJob record that includes a Job abstract operation name and any necessary argument values. When there is no running execution context and the execution context stack is empty, the ECMAScript implementation removes the first PendingJob from a Job Queue and uses the information contained in it to create an execution context and starts execution of the associated Job abstract operation.

this answer is mainly referenced from the answer on StackOverflow .

Finally, I strongly suggest JavaScript who is good at learning and good at English to look at the above specifications, and then help to see if there is anything wrong with my understanding. After all, my understanding is limited.


The

JS engine creates the execution context stack ECS to manage execution context
when the application ends, the stack is cleared.
so until the end, there is always a global execution context at the bottom of the stack

I don't know the second question. I look forward to answering it.


my understanding is that the execution context contains the global context


when JavaScript starts to interpret the execution code, the first thing it encounters is the global code, so it will first push a global execution context into the execution context stack, which will exist at the bottom of the execution stack throughout the program life cycle, and the stack will not be emptied until the end of the entire application.
you can refer to my article
execution context in javaScript

Menu