Skip to content
Lydia Hallie theavocoder
Course contents
Complete JavaScript / Lesson 15

Syntax parser

Your computer can’t read JavaScript code. When you run JavaScript code, a compiler converts your code into a set of instructions that machines can understand. The syntax parser is part of this compiler. It goes through your code character by character, and determines if the syntax is valid JavaScript or not before executing your code.

Syntax parser diagram 1

In this case, it saw that the characters “f” “u” “n” “c” “t” “i” “o” and “n” declared a function. Now, it expects a certain type of behavior: if there would’ve been an extra character, for example “functiona”, it would throw an error.

The syntax parser parses this into an AST (Abstract Syntax Tree). Although the real AST is way more complex, a way to visualize what the parser would produce in this case is:

Syntax parser diagram 2
Syntax parser diagram 3

An example of how an actual tree looks, can be seen using esprima.

Syntax parser diagram 4

You can also find more examples on its online editor https://esprima.org/demo/parse.html#.

References and further reading