In modern software development, JavaScript has become a widely used programming language, and it can be found in the fields of web front-end development, back-end development, mobile application development, etc. In this article, we will start from the JavaScript language to reveal some basic and key concepts in this language and its related ecology, as well as how they were generated in the historical wave of the Internet.
1.JavaScript
JavaScript, or JS, is an advanced, interpreted programming language. Compared with the compiled languages we commonly use, such as C and C++, JavaScript is special in that it is a dynamic interpreted language. Its feature is that it does not need to be compiled into machine code in advance when it is executed, but is read and executed line by line by the interpreter.
Although JavaScript has Java in its name, there is no direct relationship between the two languages, but an indirect relationship. This should start with the original intention of JavaScript development. In the past, when there was no web scripting language, all operations on the web page had to be sent to the server and then returned, which was inefficient. Therefore, a scripting language embedded in the web page was needed to control some simple browser behaviors, such as doing some local checks. Therefore, Netscape and Sun cooperated to develop a browser scripting language called LiveScript, which was later renamed JavaScript when it was officially released. Sun is the inventor and owner of the Java language.
JavaScript borrowed some of the syntax of Java, but the syntax is more unrestrained and free, and there are also great differences in other aspects, mainly because the application environments of the two are very different.
2.Node.js
Node.js is a JavaScript runtime environment that is used to execute JavaScript code.
Why does such a runtime environment exist? From the original intention of JavaScript development, it can be seen that it is intended to run in the browser to make web page interaction more convenient. However, as the influence of JavaScript continues to grow, its scope of use has changed. In 2009, Node.js was released. The first version supported basic network communication and file system operations, which allowed JavaScript to be used on the server, marking that JavaScript can be used for server-side programming.
In Node.js, node refers to a node. Here we can use it to describe an environment, and the .js at the end means that this is for JavaScript to prevent confusion with other node projects. However, as Node.js becomes more and more well-known, it is now almost directly referred to as node.
The emergence of Node.js has promoted the full-stack development of JavaScript, allowing developers to write front-end and back-end code in the same language.
In addition to providing a JavaScript runtime environment, Node.js also provides a rich module library that can be directly introduced during development, making development more efficient and convenient.
Node.js adopts an event-driven, non-blocking I/O model, which means that it can handle a large number of concurrent connections on a single thread. Traditional server programming models are usually multi-threaded or multi-process, and each connection requires an independent thread or process to handle, which will cause a large performance loss when handling a large number of concurrent connections. Node.js implements non-blocking I/O operations through event loops and callback functions, allowing a single thread to handle more concurrent connections, thereby improving overall performance.
3.JavaScript engine and V8 engine
Why can Node.js run JavaScript? Because it has a built-in JavaScript V8 engine, which is an engine developed by Google.
So what is an engine? The engine mentioned here refers to the JavaScript engine, which is simply the runtime environment that can process and execute Javascript code. The engine includes interpreters, compilers, and other parts.
The reason why browsers can run JavaScript is that each browser has its own JavaScript engine, as follows:
Browser |
JavaScript Engine |
Netscape Navigator |
Netscape JavaScript Engine (The first JavaScript engine, later renamed SpiderMonke) |
Google Chrome |
V8 |
Mozilla Firefox |
SpiderMonke |
Safari |
JavaScriptCore (Later switched to Apple Nitro engine) |
Microsoft Edge |
Chakra (Later changed to use V8) |
Opera |
Carakan (Later changed to use V8) |
…… |
|
It can be seen that the JavaScript engine is embedded in the browser, which was the most common mode in the past. Until the emergence of Node.js, JavaScript was able to develop independently of the browser and became cross-platform, allowing it to run on different operating systems.