Home

April 22, 2026

Parsing JavaScript: From String to AST

A dive into how JavaScript V8 engine parses code

Parsing JavaScript: From String to AST

When you run JavaScript in Node.js, you might think your code is executed directly, but it’s not.

Under the hood, a lot of processes happen before your code actually runs. We’ll explore this step by step throughout this series. But first, why is it important to know all of this?

As you go through these posts, you’ll see practical examples and learn how to write code that runs significantly faster. More importantly, you’ll understand why it performs better.

If you work on applications that handle a large number of executions every day, this knowledge can help you ship better-performing systems. So, let’s get started.


What does “Parsing” mean?

Before the code can be executed by V8, it needs to be understood. At this point, your code is just a string of characters:

function add(a, b) {
  return a + b;
}

For us, it clearly represents a function, but for the engine, it’s just text. So the first step is when V8 parses this and transforms it into something it can clearly understand and work with.


AST

The result of this parsing process is called an AST (Abstract Syntax Tree), which is a data structure used to represent the structure of a program or code snippet. It is basically a tree that represents your source code.

In our example, the add function will be parsed into something conceptually like this:

{
  "type": "FunctionDeclaration",
  "name": "add",
  "params": ["a", "b"],
  "body": {
    "type": "ReturnStatement",
    "expression": {
      "type": "BinaryExpression",
      "operator": "+",
      "left": "a",
      "right": "b"
    }
  }
}

Lazy Parsing

A cool thing is that V8 does not always parse all the code at once. It uses a technique called lazy parsing.

This is really cool because:
  • Only the necessary parts of the code are parsed initially
  • Functions may be parsed only when they are actually used
This helps reduce startup time, especially in large applications.

You can read more about it here: https://v8.dev/blog/preparser


This series is based a lot on the official V8 blog, which has amazing content about how the engine works under the hood. If you want to go deeper, it’s definitely worth checking out: https://v8.dev/blog


What comes next?

After parsing, this AST will be transformed into bytecode, which will be executed by V8’s interpreter called Ignition.