1. Hand-painted a picture description.
2. Why JavaScript is single-threaded (quote Nanyi teacher here)
A single thread of JavaScript, which is related to its purpose.
As a browser scripting language, the main purpose of JavaScript is to interact with the user and manipulate the DOM.
This determines that it can only be single-threaded, or it can create complex synchronization problems.
For example, assuming that JavaScript has two threads at the same time, one thread adds content to one of the DOM nodes, and another thread deletes the node, which thread should the browser use?
Therefore, in order to avoid complexity, from the birth, JavaScript is a single thread, which has become the core characteristics of the language, will not change in the future.
To take advantage of the computational power of multi-core CPUs, HTML5 proposes web worker standards that allow JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.
So, the new standard doesn't change the nature of JavaScript single-threaded threads.
Portal: JavaScript operation mechanism of the event loop (events Loop) detailed
Where is the 3.JavaScript asynchronous representation?
Like the beginning of the figure, the person thinks the left main thread is synchronous, the left event queue (Message queue) is asynchronous.
Of course, there are many asynchrony in javascript:
Ajax (XMLHttpRequest)
Image tag,script tag,iframe (principle similar)
settimeout/setinterval
CSS3 transition/ Animation
postMessage
Web Workers
Web Sockets and more
...