JavaScript – Interpreted or Compiled?

Low level language(machine language):

The computer language consist of 0,1, so think that you need to write a program using 0,1 digits so the computer can do what you want, it’s very complicated even for the most easiest program.

So thanks to scientists who take cares of building computer languages (machine language => assembly language => high-level language) in order to be easy for developers to write programs in high-level languages, including BASIC, COBOL, C, C++, c#, Java, Pascal, PHP, Ruby, Python, JavaScript….

Programming Languages are considered as high-level because they are easy to use,write,read and closer to human languages.
But computers doesn’t understand the code we write in high-level languages, and because of that there are processes done moving from high-level down to low level languages, so as to computer’s hardware can execute our programs.

Program written in a high-level language must be translated into machine language, and this can be done by a compiler or interpreter.

Interpreted languages: PHP, Ruby, Python, Perl… [you can look further at Wikipedia]

Compiled languages: Lisp, BASIC, C, C++, Objective-C, Swift… [Wikipedia-Compiled languages]

What is compiled language?

Let’s say you run a source code that you programmed in C programming language, then the code must be compiled first by a compiler, so if there are a syntax errors in your program, your code will not be compiled until there are no errors.
So if you want a user to use your program, then you can’t simply pass the source code you wrote in C to the user, instead you have to pass the compiled code that is considered as executable machine binary code, so the computer will run the machine code on its own.
And there is no need to recompile the program after receiving the machine binary code in order to run the program in the user’s computer.

What is interpreted language?

This languages depends on an interpreter program, so the source code is not directly run by the target machine, instead interpreter program reads and then executes the original source code.
And interpreters in most cases run your code line by line so if there are an errors in your program it will not be recognized by the interpreter until it reach the error line of code.
Bash script is interpreted language, which means when it’s running line 3 for example, it has absolutely no idea what to expect on line 4, it hasn’t even looked at line 4 yet, it’s not there yet, so it’s dealing with line 3.
So an interpreter literally goes from top to bottom, now there’s obviously some looping mechanisms and other things that interpreters can do through goto and things like that, but interpreted languages go top to bottom just line at a time.

Does JavaScript interpreted or compiled language?

Unlike to what mentioned in Wikipedia nowadays JavaScript actually does have a compiler, but it’s not quite the same as the compiler of C++, C or things like that, so in JavaScript we don’t send out the binary compiled code to users in order to run it like we do with C++, C. In c++ for example, we compile it, and we send the “exe” file to users in order to run it. What we send in JavaScript is the original source code, and it’s compiled every single time that it’s run.
So unlike Bash interpreted language (and other interpreted languages) for example, it has looked at line 4 before it starts to run line 3.
So the compiler scan the source code of JavaScript, and it does initial pass through the source code to compile it.
This last explanation based on what Kyle Simpson said, the author of series books called :You don’t know JS


Short animated YouTube video that explains compiler vs interpreter

Another related article about Interpreter or Compiler in context of JavaScript

Stckoverflow- Why does Java code need to be compiled but JavaScript code does not

The race for speed part 1: The JavaScript engine family tree

The race for speed part 2: How JavaScript compilers work

Thanks for reading this post, I hope it give you some direction to understand if JavaScript has compiler or not.
If you think there is some wrong information in this post then please correct me, and I will be glad to update the post.

mhmd

Hello, I'm Mohammad Daka, software engineer who love coding. In my spare time I blogs about JavaScript,PHP, Chrome Extensions, helpful resources and tools, I'm also working on my own projects. Don't hesitate to contact me at my twitter account: @mohammad_daka :)

You may also like...