Skip to main content

Command Palette

Search for a command to run...

What is Node.js? JavaScript on the Server Explained

Updated
•8 min read•View as Markdown

JavaScript is one of the most popular programming languages in the world. For many years, it was mainly known as a language used inside web browsers to make websites interactive. It could handle things like button clicks, form validation, animations, and changes to web pages.

However, modern JavaScript is no longer limited to browsers. With the introduction of Node.js, developers became able to run JavaScript outside the browser and use it for backend development.

But what exactly is Node.js, and how did it make JavaScript a server-side technology? To understand this, we first need to understand the difference between JavaScript as a programming language and the environment in which it runs.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside a web browser.

An important point here is that Node.js is not a programming language. JavaScript is the programming language, while Node.js provides the runtime environment needed to execute JavaScript outside the browser.

For example, Python code requires a Python runtime or interpreter to execute, and Java programs run using the Java Virtual Machine (JVM). Similarly, Node.js provides an environment where JavaScript can run on a computer or server without requiring a browser.

This allows developers to use JavaScript for tasks such as creating web servers, building APIs, communicating with databases, working with files, and developing command-line tools.

Why JavaScript Was Originally Browser-Only

JavaScript was originally created to make web pages interactive. When a user opened a website, JavaScript code was downloaded and executed by the browser.

The browser provided JavaScript with useful features known as Web APIs. For example, JavaScript could interact with the HTML page through the Document Object Model (DOM), respond to user events, use timers, and make network requests.

However, browser JavaScript was intentionally restricted from directly accessing many parts of the user's computer for security reasons. A website should not be able to freely read files from a user's computer or control the operating system.

Because of this, JavaScript was mainly considered a client-side language.

Developers who wanted to build backend applications traditionally used technologies such as PHP, Java, Python, Ruby, or other server-side languages.

This changed significantly with Node.js.

How Node.js Made JavaScript Run on Servers

Node.js provided JavaScript with a runtime environment outside the browser.

Instead of relying on browser APIs, Node.js provides its own APIs that are useful for server-side development. These allow developers to perform operations such as reading and writing files, creating HTTP servers, communicating over networks, and interacting with the operating system.

This created an important shift in web development.

Developers could now use JavaScript on both sides of a web application.

On the frontend, JavaScript could run inside the browser and handle user interactions.

On the backend, JavaScript could run through Node.js and handle requests, communicate with databases, process data, and send responses back to clients.

A simple analogy is to think of JavaScript as a worker with the same skills but working in two different workplaces.

Inside the browser, the worker's job is to interact with the web page and the user.

Inside Node.js, the worker's job is to work with servers, files, networks, and backend systems.

The language is still JavaScript, but the environment and available capabilities are different.

JavaScript Language vs JavaScript Runtime

The difference between a programming language and a runtime environment can sometimes be confusing.

JavaScript defines the language itself, including concepts such as variables, functions, objects, classes, loops, and promises.

A JavaScript runtime provides the environment needed to actually execute JavaScript code and gives the code access to additional features.

A browser is one type of JavaScript runtime environment. It provides browser-specific features such as the DOM.

Node.js is another JavaScript runtime environment. It provides server-side features such as file system access, networking, and process management.

Therefore, the JavaScript language remains mostly the same, but what JavaScript can interact with depends on the runtime environment.

The V8 Engine

At the core of Node.js is the V8 JavaScript engine, originally developed by Google for the Chrome browser.

V8 is responsible for executing JavaScript code. At a high level, it takes JavaScript code and converts it into instructions that the computer can execute efficiently.

Node.js uses V8 outside the browser and combines it with additional features required for server-side programming.

However, Node.js is more than just the V8 engine. V8 handles JavaScript execution, while Node.js provides additional APIs and infrastructure that allow JavaScript to interact with files, networks, operating systems, and other backend resources.

We do not need to understand the deep internal details of V8 to use Node.js effectively. The important idea is that V8 provides the high-performance JavaScript execution engine that helps Node.js run JavaScript outside the browser.

Event-Driven Architecture

One of the important ideas behind Node.js is its event-driven architecture.

In a traditional blocking approach, a program may start an operation and wait until that operation finishes before moving to the next task.

Node.js is designed to avoid unnecessary waiting for many I/O operations.

For example, suppose a Node.js server receives a request that requires reading a file. Instead of blocking the main JavaScript thread until the entire file is read, Node.js can start the operation and continue handling other work.

When the file operation is completed, Node.js can process the result and continue the appropriate task.

This event-driven and non-blocking approach makes Node.js particularly useful for applications that need to handle many simultaneous connections or I/O operations.

Node.js Compared with Traditional Backend Runtimes

Before Node.js became popular, backend development commonly used technologies such as PHP and Java.

In a traditional PHP setup, a web server receives a request, executes the PHP code associated with that request, generates a response, and sends it back to the client. PHP has historically followed a request-response execution model, although modern PHP servers and frameworks can support more advanced architectures.

Java backend applications often use multiple threads to handle many requests. Each thread can process a request independently, which can provide parallel execution but also requires careful management of threads and system resources.

Node.js takes a different approach. It mainly executes JavaScript on a single main thread and uses an event-driven, non-blocking model for handling many I/O operations concurrently.

This does not mean that Node.js is always better than PHP or Java. Each technology has its own strengths and is suitable for different types of applications.

One major advantage of Node.js, however, is that web developers can use JavaScript across the entire application stack. A developer who already knows frontend JavaScript can use the same language to build backend applications.

Why Developers Adopted Node.js

One of the biggest reasons developers adopted Node.js was the ability to use JavaScript for both frontend and backend development.

Before Node.js, a web developer might use JavaScript in the browser and then switch to PHP, Java, Python, or another language for backend development.

With Node.js, development teams could use JavaScript across both sides of an application. This made it easier to share knowledge, development practices, and sometimes even code between frontend and backend projects.

Another major reason is Node.js's asynchronous and non-blocking architecture. Many modern web applications perform large numbers of I/O operations, such as calling APIs, accessing databases, and handling network connections.

Node.js is designed to handle these types of workloads efficiently.

The large ecosystem around npm, the package manager commonly used with Node.js, also contributed significantly to its popularity. Developers can use packages created by the community instead of building every feature from scratch.

Real-World Use Cases of Node.js

Node.js can be used for many different types of applications.

It is commonly used for building REST APIs and backend services, where the server needs to receive requests, process data, interact with databases, and return responses.

Node.js is also useful for real-time applications, such as chat applications and collaboration tools, where information needs to move quickly between clients and servers.

It can also be used for streaming applications because of its ability to process data efficiently as it becomes available.

Other common use cases include microservices, command-line tools, serverless functions, automation scripts, and backend systems for web and mobile applications.

However, Node.js may not always be the best choice for applications that perform extremely heavy CPU-intensive calculations directly on the main thread. Such work may require worker threads, separate processes, or other specialized technologies.

Conclusion

Node.js changed the role of JavaScript in software development by allowing it to run outside the browser.

JavaScript is the programming language, while Node.js is a runtime environment that provides the tools and capabilities required to execute JavaScript on servers and computers.

By combining the V8 JavaScript engine with server-side APIs and an event-driven, non-blocking architecture, Node.js made it possible to build scalable backend applications using JavaScript.

One of the main reasons developers adopted Node.js is its simplicity for web development. The same language can now be used in the browser to create interactive user interfaces and on the server to handle backend logic.

In simple terms, JavaScript originally worked mainly inside the browser. Node.js gave JavaScript a new workplace—the server—and this helped JavaScript grow from a browser scripting language into a language used across the entire web development stack.