DEV Community

Swastik Upadhye
Swastik Upadhye

Posted on • Updated on

Javascript : Interpretated or Compiled ?

Introduction

This is a long-debated question. The most of the community goes with the interpreted language. But what is interpreted language ? Does it fit entirely to the defination of interpreted language ? We are going to touch some of the key concepts : Compilation, Interpretation, Parsing, AST, etc. Let's find out one by one.

Compilation

The computer understands only 0s and 1s which is called as machine code. So at the final stage every computer program must be converted into this machine code.
The program or the source code written by the user goes through various stages and it is called either compilation or interpretation.

In the compilation process, the entire source code is converted into a machine code at once. And it produces a binary code which is portable that can be executed later on.

When it comes to Javscript we don't really see this kind of behaviour/mechanism right.

But this is not the pefect base reason that we can go for final conclusion.

Moreover, how Javascript handles the errors plays key role to conclude upon whether it is compiled or interpreted.


Interpretation

In the Interpretaion, there is an interpreter which runs and executes the code line by line. So program is running and executed at the same time.

So to simply it. Let us consider that we have 5 lines of code.
In the Interpretation, the code is being executed frome line no. 1 to 5 sequentially.
Now if there's an error due to change in value at runtime that cannot be detected early.

So does this conclude that JS is not an Interpreted language ?

Now it sounds so confusing and really hard to conclude right. We should understand what is parsing and what are parsed languages ?


Parsing

Some languages undergo through processing step before executing which is nothing but parsing.

So unlike Interpreted languages if there's any error at line no.3 It can be detected in the parsing phase itself before execution.

Now the big question is what is the difference between parsed language and compiled language.

It is to be noted that :

All compiled languages are parsed

When any program is parsed completely then after the parsing it outputs the program in a specific parsed form which is called as Abstract Syntax Tree (AST)

Let's consider the following example :

var a = 10;

Enter fullscreen mode Exit fullscreen mode

Equivalent AST for above program :

Image description

Note : We can check AST generated for our source code here : AST


What happens in case of JS ?

  • We can observe that there is an early error detection mechanism when we try to execute the Javascript program

  • So this infers that Javascript program is being parsed before its execution.

  • Thus, Javascript is a Parsed Language

  • Now this parsed Javascript code is converted to some kind of byte-code by Javascript engine.

  • Furthermore this byte code is refined and optimized by JIT compiler and then Javscript VM executes it.

Conclusion

Although Javascript looks like an interpreted language at first glance.
It is clear now it goes through various phases as stated above.
Javascript is a mixed approach of Compiled and Interpretated Language.

  • Thank you for stopping by!. If you find it useful consider sharing it. You can connect with me here : LinkedIn

Top comments (0)