Abstract Syntax Tree(AST) and how it works with ReactJS

Jonathan Kong
2 min readMar 17, 2023

An Abstract Syntax Tree (AST) is a tree-like data structure that represents the structure and hierarchy of source code in a programming language. Each node in the tree corresponds to a programming construct, like variables, functions, or expressions. ASTs are used by compilers and other tools to understand, analyze, and transform the code.

In simple terms, imagine you have a sentence that you want to understand. You break it down into words and phrases, then arrange them in a tree structure to show their relationships. An AST does the same thing but for programming languages.

And… what has it got to do with ReactJS?

ReactJS is a popular JavaScript library used for building user interfaces, especially single-page applications. React components are written using JavaScript and JSX (a syntax extension that allows you to write HTML-like code within JavaScript).

When you write a React component using JSX, it gets transformed into JavaScript before being executed in the browser. This transformation process involves creating an AST of the JSX code to understand its structure, then generating corresponding JavaScript code from the AST.

To summarize, the Abstract Syntax Tree (AST) is a tree-like representation of your source code that helps tools like compilers understand and process the code. In the context of ReactJS, ASTs play a key role in transforming your JSX code into JavaScript, which is then executed in the browser to create the desired user interface.

--

--