Is JavaScript Interpreted or Compiled?

You have probably read that Javascript is an interpreted language, you've also probably read that it's not an interpreted language and that it's actually a compiled language.

Are you confused?

I surely was confused, because I would get different answers each time I read an article or book. However, the answer is that it actually depends on the implementation.

Let me explain.

A program must be translated so it's understood by a computer before we can run it. Javascript code is executed by a Javascript engine. The engine makes sure that what you've written is understood by the machine.

An interpreter does this during runtime and executes statement by statement. A compiler translates beforehand and requires more time, but this allows the compiler to optimize and give us a fast execution later on when we run the code.

The first Javascript engines were only interpreters. As Javascript became, more commonly used, the loss of performance caused by interpretation (amongst other things of course) gave birth to new engines.

These modern Javascript engines use a JIT (just-in-time) compilation. A just-in-time compiler doesn't compile the same way a compiler compiles for example C++. Compilers for other languages often have lots of time to optimize during compilation, but just as the name implies, that's not the case with JIT (just-in-time) compilation. Instead just about when the Javascript code is supposed to run, it gets compiled to executable bytecode.

So, now you should have a better understanding as to why people are confused about whether or not Javascript is an interpreted language or not. And why they refer to Javascript as being an interpreted language and also sometimes a compiled language.

Further reading:

Now that you know that modern Javascript engines use JIT, I would highly recommend reading the following articles for a more in-depth dive about JIT:

  1. A crash course in just-in-time (JIT) compilers
  2. JavaScript essentials: why you should know how the engine works
  3. How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code