A Javascript engine is a program that executes JavaScript code, a popular one is Google V8 engine.
It is composed by two main components the heap and callstack.
The heap is used for memory allocation of variables and objects, at the top of the current scope, a process called hoisting.
During the phase of function execution, an execution context is created, which includes:
- a global object window (in case of the browser)
- a special object called 'this' and
- a ref to the outer environement.
A callstack is a list that tracks functions that have been called during the execution of the code.
JavaScript it is single threaded and it has one CallStack.
If a function is called is pushed at the top of the call stack list, and when it is executed, it's removed from the list.
Asynchronous tasks are placed in a so called callback queue and executed only when the call stack is empty.
The job of the event loop is to check if the call stack is empty and in this case it executes tasks from the callback queue.
Web API calls are added from callstack to the web api container until an action is triggered (e.g. DOM, ajax call, button click).
Once an action is triggered, a callback function is added to the callback queue, and executed when the call stack is empty.

Top comments (0)