Many of the answers that I found on the internet made me as Confused as,
Few of the other online posts, discussions, blogs shed some bright lights on it. I thought it would be better to put it in a concise and easy to understand manner here.
- What is the difference between an Interpreter and a Compiler?
As mentioned in Wikipedia,
An interpreter is a computer program that directly executes instructions written in a programming or scripting language without requiring them previously to have been compiled into a machine language program. It translates one Statement at a time.
- Tokenizing: Breaking up a source code string into meaningful chunks called,
Tokens. For example, the source code var age = 7; can be tokenize as, var, age, =, 7 and, ;.
- Parsing: Parsing is a methodology to take the array of Tokens as input and turn it into a tree of nested elements understood by the grammar of the programming language. This tree is called Abstract Syntax Tree(AST).
- Code Generation: In this phase, the AST is used as input, and an executable byte-code is generated that is understood by the environment(or platform) where the executable code will be running. The executable byte-code is then refined/converted even further by the optimizing JIT (Just-In-Time) compiler.
"A Picture worth thousand words". Here is a pictorial representation of how these three phases take place:
You can use the AST Explorer tool to see how code written by you gets parsed into an Abstract Syntax Tree(AST).
Created using AST Explorer
compilation process produces a binary byte code, the
JS virtual machine executes it.
As a developer, we are abstracted away from all of these. We will see more in-depth topics about
It's time. Let's call it out, JS is a Compiled Language.
The resource I recommend for this subject is,
This is an excellent read if you are looking for more detailed explanations. You can start reading from this section directly. My favorite part from this read is,
So what do "parsed" languages have in common with "compiled" languages? First, all compiled languages are parsed. So a parsed language is quite a way down the road toward being compiled already. In classic compilation theory, the last remaining step after parsing is code generation: producing an executable form.
I hope you find the article useful. Please Like/Share so that it reaches others as well. If you enjoyed this article or found it helpful, let's connect. You can find me on Twitter(@tapasadhikary) sharing thoughts, tips, and code practices.
To get e-mail notifications on my latest posts, please subscribe to my blog by hitting the Subscribe button at the top of the page.