To understand Node.js, you need two pictures in your head: what a server does, and how a JavaScript engine turns your code into something a computer can run. This chapter gives you both.
In this chapter you'll learn
- what a server is, and what "client" and "request/response" mean
- why running JavaScript on the server was a big deal
- what a JavaScript engine is, and how V8 fits inside Node.js
- how high-level code becomes machine code: interpreters, compilers and JIT
Before you start: read Chapter 01: Introduction to NodeJs.
What is a server?
A server is a computer (or a program on a computer) that provides something to other computers over a network. The computers asking for things are called clients.
- When you open
youtube.com, your browser is the client. - YouTube's machines that send back the page and the video are the servers.
The conversation always has the same shape:
- The client sends a request ("give me the home page").
- The server does some work (reads files, queries a database).
- The server sends back a response (the HTML, some JSON, an image, or an error).
On the web, requests and responses follow a set of rules called HTTP (HyperText Transfer Protocol). You'll build your own HTTP server from scratch in Chapter 11: Creating the Server.
"Server" can mean the machine (hardware in a data centre) or the program that answers requests (like a Node.js app). In this course it almost always means the program. Chapter 11: Creating the Server covers both meanings.
What a backend server usually does
- Receives requests from browsers and mobile apps
- Checks who the user is (authentication, see Chapter S2 10: Authentication, JWT & Cookies)
- Reads and writes data in a database (see Chapter 12: Databases SQL and NoSQL)
- Applies business rules ("a user can't send a connection request to themselves")
- Sends back responses, usually as JSON
Why JavaScript on the server was a big deal
Before Node.js, a typical web team used two languages: JavaScript in the browser and something else on the server (PHP, Java, Ruby, Python...).
With Node.js:
- One language for the whole stack. Frontend developers can write backend code, and code (like validation rules) can be shared.
- JSON everywhere. APIs usually send JSON (JavaScript Object Notation), which JavaScript reads natively.
- An efficient model for web traffic. The event-driven, non-blocking design from Chapter 01: Introduction to NodeJs handles many slow connections with very little memory.
This is what made the MERN stack (MongoDB, Express, React, Node.js) possible: JavaScript from the database queries to the buttons on screen. It's the stack you'll build DevTinder with in Season 2.
What is a JavaScript engine?
Your CPU doesn't understand JavaScript. It understands only machine code: very low-level instructions like "add these two numbers" or "copy this value to memory".
A JavaScript engine is a program that takes JavaScript and makes the CPU execute it. Every browser has one:
| Engine | Used in | Written in |
|---|---|---|
| V8 | Chrome, Edge, Node.js, Deno | C++ |
| SpiderMonkey | Firefox | C++ / Rust |
| JavaScriptCore | Safari, Bun | C++ |
How V8 fits inside Node.js
Here's the key fact: Node.js is a C++ program that embeds V8.
- V8 knows how to run JavaScript, and nothing else. On its own it has no
setTimeout, no files and no network. - Node.js wraps V8 and adds the "superpowers" a server needs:
fsfor files,httpfor servers,crypto,processand more. Many of these are written in C++ and exposed to your JavaScript.
ECMAScript is the official standard that defines the JavaScript language. V8 implements ECMAScript. Things like console.log, setTimeout and require are not part of ECMAScript: browsers and Node.js each add them. (import / export are part of ECMAScript, so V8 itself understands them; Node.js only finds and reads the files. Chapter 05 shows how.) That's why setTimeout exists in both, while fs exists only in Node.js.
From high-level code to machine code
High-level vs low-level languages
- High-level languages (JavaScript, Python, Java) are written for humans: readable words, objects, functions.
- Low-level languages (assembly, machine code) are written for the CPU: numbers and registers.
Something must translate one into the other. There are three ways to do it.
Interpreter
An interpreter reads your code and executes it line by line as it goes.
- ✅ Starts running immediately
- ❌ Slower overall, because it never produces optimized machine code
Compiler
A compiler translates the whole program into machine code before it runs (like C or C++).
- ✅ The resulting program runs fast
- ❌ You must wait for compilation before anything runs
JIT (Just-In-Time) compilation: the best of both
JavaScript engines use a JIT compiler. Code starts running right away in an interpreter, and the engine compiles the frequently used parts to optimized machine code while the program is running.
JavaScript is therefore neither "interpreted" nor "compiled". It's JIT-compiled. This is a popular interview question.
V8's pipeline, in short
The main steps:
- Parsing. V8 breaks your code into tokens (keywords, names, operators) and builds an AST (Abstract Syntax Tree), a tree that represents your code's structure. Syntax errors are found here.
- Ignition (V8's interpreter) turns the AST into bytecode and runs it straight away.
- While the code runs, V8 profiles it: which functions are called often ("hot") and which types they see.
- TurboFan (V8's optimizing compiler) compiles hot functions to optimized machine code.
- If an assumption breaks (a function that always got numbers suddenly gets a string), V8 deoptimizes and goes back to bytecode.
V8 also has a garbage collector that frees memory your program no longer uses. It frees memory that is unreachable; if your code still holds a reference by mistake (an ever-growing array, a listener that is never removed), that memory is never freed. That's still a memory leak, even with a garbage collector.
We go deeper into each step (and two newer compiler tiers, Sparkplug and Maglev) in Chapter 08: Deep dive into v8 JS Engine.
Key takeaways
- A server answers requests from clients with responses, usually over HTTP.
- Node.js made it possible to use one language (JavaScript) across frontend and backend.
- A JavaScript engine turns JavaScript into machine code. Node.js uses V8.
- Node.js = V8 + Node APIs (fs, http...) + libuv, written mostly in C++.
- JavaScript is JIT-compiled: interpreted first (Ignition), with hot code optimized (TurboFan).
- The garbage collector frees unreachable memory; it can't fix references you keep by mistake.
Interview questions
Q1. What is the V8 engine?
V8 is Google's open-source JavaScript and WebAssembly engine, written in C++. It's used in Chrome and Node.js. It parses JavaScript, runs it with the Ignition interpreter, and compiles frequently used code to optimized machine code with the TurboFan JIT compiler. It also manages memory with a garbage collector.
Q2. Is JavaScript interpreted or compiled?
Both. Modern engines use JIT (Just-In-Time) compilation. V8 first runs code as bytecode in an interpreter so it starts quickly, then compiles hot functions to optimized machine code while the program runs. Calling it "an interpreted language" is outdated.
Q3. What is the difference between an interpreter and a compiler?
An interpreter executes code line by line as it reads it: fast to start, slower to run. A compiler translates the whole program to machine code before running it: slower to start, faster to run. A JIT compiler mixes both by compiling only the frequently used parts, at runtime.
Q4. What is an AST?
An Abstract Syntax Tree is a tree representation of your code's structure that the parser produces. For const a = 1 + 2, the tree has a variable declaration node, with a binary expression (+) node whose children are the literals 1 and 2. Engines, linters (ESLint), formatters (Prettier) and compilers (Babel, TypeScript) all work on ASTs.
Q5. What is ECMAScript, and how is it related to JavaScript and Node.js?
ECMAScript is the standard that defines the JavaScript language (syntax, types, Promises, classes...). JavaScript is the most well-known implementation of it. V8 implements ECMAScript, and Node.js adds non-standard APIs on top, like fs, http and require. That's why those exist in Node but not in the language standard. Modules written with import / export, on the other hand, are standard: V8 parses and links them, and Node.js supplies the files.
Q6. Can you have memory leaks in JavaScript if it has a garbage collector?
Yes. The garbage collector only frees objects that are no longer reachable. Common leaks are global arrays or caches that keep growing, event listeners that are never removed, and timers (setInterval) that are never cleared but keep references alive.
Q7. What's the difference between a client and a server?
A client starts the conversation by sending a request (a browser, a mobile app, Postman). A server listens for requests and sends back responses. The same machine can be both: your Node.js API is a server to the browser, but a client of the database.
What's next
Enough theory for now. In Chapter 03: Let's Write the code you'll install Node.js, try the interactive REPL, and run your first JavaScript file outside the browser.