JavaScript — is it Compiled or Interpreted?

So You Think JavaScript is Interpreted? According to most of the internet, JavaScript is an interpreted language, but that’s not necessarily true. Here’s Why.

Almog Adziashvili
4 min readJan 16, 2020
image from fossbytes.com

We all know the common question every programmer has to ask about the programming language he learns — is it compiled or interpreted.

Let’s first define what each of those means.

Compilation vs. Interpretation

There’s a lot of information about each of those procedures so I will keep it simple:

Compiled language — the source file typically will be “compiled” to machine code (or byte code) before being executed.

Interpreted language — the source code will be read and directly executed, line by line.

And like everything in life, I strongly believe that you can better understand it with alcohol.

Let’s say you want to make a Mojito, you go to Google (or Bing if you are a freak) and search for the ingredients list, this is what you came up with:

· The juice of 1 Lime
· White rum
· Mint
· Soda water

There are 2 ways to make the cocktail, the Compiler or the Interpreter way.

The compiler will first, before doing any mixing, organize all the ingredients in front of him, the specific amounts of every single ingredient, only then, will he mix all the ready components of the cocktail.

The interpreter will take his glass and will start by reading the ingredients, line by line. he will go to his refrigerator and will fetch a lemon, cut it and squeeze it directly into the glass, then pour the white rum, etc.

The build (preparation) time of the compiler will be longer than the interpreters. however, the run (mixing) time will be much shorter.

Now that you know the difference let’s talk about JavaScript.

So — is JavaScript Compiled or Interpreted

Well, Like many of you, when I started learning JavaScript I’ve been told that JavaScript — like most scripting languages is an interpreted language, and lived with this presumption in peace.

Read the following paragraph published at web.stanford.edu:

JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run.

According to most of the internet, JavaScript is an interpreted language, but that’s not necessarily true.

For Example, the V8 engine, the engine that runs Google Chrome and NodeJS, compiles to native code internally:

V8 increases performance by compiling JavaScript to native machine code before executing it, versus executing bytecode or interpreting it.

Also, Rhino and TraceMonkey use compilation as part of their process:

TraceMonkey adds native‐code compilation to Mozilla’s JavaScript® engine (known as “SpiderMonkey”).

For example, look at this program:

console.log('Hippity Hoppity');
oops oops;

In theory, an interpreter would read the first line, print “Hippity Hoppity” and only then throw a Syntax Error.

But for modern JavaScript’s runtime environments, this is not the case, immediately after running the program, before executing the log function, it crashes.

Another example is Hoisting, consider:

max(1, 2);
// 2
function max(num1, num2){
return num1 > num2 ? num1 : num2;
}

How does the JS engine know about the ‘max’ Function before it “reaches” to the deceleration? Again, the only reasonable answer to this question is that the code must first be compiled before execution.

So, JavaScript is a compiled language, right?

Well, it’s complicated. in the past, every programming language was fairly easy to categorize as one or the other, but the modern approach of running the source code created a sort of “in-between” area.

Of course, the result of compilation is not portable among various JS engines. Also, JS is not compiled well in advance, like traditional compiles language. But, modern JS engines perform similar steps as other compilers.

Consider the flow of a program:

1. The source code gets transpiled (Babel) and packaged (Webpack).

2. The JS engine parses the code to an Abstract Syntax Tree (AST).

3. The engine converts that AST to a kind-of byte code, which is then converted even further by the JIT compiler.

4. JS VM executes the program.

Some will argue that the JS VM is “interpreting” the “byte code”, but if you say that you also say that Java (another JVM-driven language) is also interpreted.

Well?! Is it Compiled then?

The answer is closer to yes than no, but it’s a matter of perspective and implementation, I guess.

I strongly recommend you to keep exploring this topic and tell me what you think in the comments section.

And the next time you’re in front of an Interviewer and he asks you this question — just tell him compiled, explain yourself and then give him the link to this article.

My name is Almog Adziashvili, I am a Full Stack Developer from Israel.

For more articles follow me on Medium.

--

--