# What is Node.js?

Node.js is a **cross-platform, open-source JavaScript runtime environment** that allows developers to execute JavaScript code outside a web browser to build server-side applications.

To run JavaScript outside the browser, it needs a base. [**Ryan Dahl**](https://en.wikipedia.org/wiki/Ryan_Dahl), who is the developer of Node.js, embedded JavaScript in C++ that runs outside of the browser.

But here is a catch, as it is plain JavaScript outside the browser, that's why he imported **libuv,** which adds Eventloop and Threadpool.

**GIST - v8 + (c++) + libuv = Node.js**

## JavaScript runtime vs programming language difference

*   **JavaScript** is a language that consists of a set of rules, syntax, and logic that make the website functional.
    
*   **Node.js** is the environment, the "house" where the language resides, providing the tools necessary for it to interact with a computer's operating system, such as reading files or connecting to a network.
    

## Why JavaScript was Originally Browser-Only

The sole purpose of JavaScript was to make static web pages interactive (e.g., button clicks, form validation).

Web browsers such as Netscape and Internet Explorer incorporated "engines" to interpret code. However, these engines were sandboxed for security reasons, meaning they were strictly prohibited from accessing your hard drive or network directly. This restriction was implemented to prevent malicious websites from stealing data. As a result, JavaScript was unable to function as a general-purpose server language like C++ or Java due to this "locked-in" nature.

![](https://cdn.hashnode.com/uploads/covers/695291ab5b12442dcb8f69d8/0324175c-df7f-4117-b7ca-dd29ebef58f2.png align="center")

## How Node.js made JavaScript run on servers?

In 2009, Ryan Dahl took the **V8 engine,** an ultra-fast engine created by Google for Chrome, and wrapped it in a C++ layer. This development enabled JavaScript to communicate with the computer's hardware.

*   **Browser JavaScript** it act like a translator working in a **library**: They can help you read and interact with the books (the webpage), but they can’t leave the building (closed environment).
    
*   **Node.js** serves as a translator in a **workshop**: it has access to all the power tools (the file system), the shipping dock (the network), and the warehouse (the database).
    

## The V8 Engine: The Heart of Node.js

The **V8 engine** is what makes Node.js exceptionally fast. Instead of interpreting code line by line, which can be slow, it uses a process called **Just-In-Time (JIT) compilation**. This process translates JavaScript directly into machine code, the binary code of 0s and 1s that the CPU understands right before execution. This efficiency is the reason Node.js can handle heavy traffic with ease.

## Event-Driven Architecture

Most traditional backends like Java or PHP handle requests using **Multi-threading**. Which means if you have 10 tasks, the server starts 10 separate threads. If those threads are waiting for the task to be done, it consumes more memory.

Node.js uses an **Event-Driven, Non-blocking I/O** model that operates a **single thread.** When a request comes in, Node.js sends the request off and immediately moves to the next task. When the data is ready, an "event" is triggered, and Node.js circles back to finish the job.

![](https://cdn.hashnode.com/uploads/covers/695291ab5b12442dcb8f69d8/89ddba9b-0e24-48cd-a58c-2ebba71d7d9f.png align="center")

## Real-World Use Cases

Why have developers flocked to Node.js? It is an ideal choice for applications that require fast and frequent data movement:

*   **Real-Time Chats:** Applications like Slack and WhatsApp Web demonstrate the need for instant message delivery.
    
*   **Streaming Services:** Services such as Netflix effectively handle massive chunks of data.
    
*   **Single Page Applications (SPAs):** Using the same programming language (JavaScript) for both the frontend and backend streamlines the development process.
    
*   **Microservices:** The lightweight nature of Node.js makes it the preferred option for breaking down large applications into smaller, interconnected components.
    

![](https://cdn.hashnode.com/uploads/covers/695291ab5b12442dcb8f69d8/51cda807-49b3-494a-9bdd-10557fba08e6.png align="center")
