7

I am new to Web development, and I am studying JavaScript.

From a course at Stanford:

JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.

And from You Don't Know JS: Scope & Closures by Kyle Simpson:

... but despite the fact that JavaScript falls under the general category of “dynamic” or “interpreted” languages, it is in fact a compiled language.

Let’s just say, for simplicity sake, that any snippet of JavaScript has to be compiled before (usually right before!) it’s executed. So, the JS compiler will take the program var a = 2; and compile it first, and then be ready to execute it, usually right away.

And from some questions at Stack Overflow, there are some ideas like: It depend on an actual implementation of the language.

Do you have any ideas?

Community
  • 1
  • 1
haitran
  • 715
  • 1
  • 5
  • 19
  • http://stackoverflow.com/questions/9623813/is-javascript-compiled-or-an-interpreted-language – Thilo Nov 03 '14 at 07:24
  • @Thilo, I have read this yet. But how about the book and the course's definition? – haitran Nov 03 '14 at 07:25
  • Can you paste these two definitions here? – Thilo Nov 03 '14 at 07:29
  • Yes, I'm already quoted those definitions in my questions, but if you need, I will edit with the full version! – haitran Nov 03 '14 at 07:34
  • Do you have the next sentence from that book, where they actually explain this assertion about "compiled language"? – Thilo Nov 03 '14 at 07:42
  • The description of JIT is wrong. Javascript is and has always been compiled to bytecode since the first prototype implementation (LiveScript). JIT refers to compiling that bytecode to native machine code - same meaning as Java's use of the term JIT – slebetman Nov 03 '14 at 07:45
  • The next sentences from this book: "*It is not compiled well in advance, as are many traditionally compiled languages, nor are the results of compilation portable among various distributed systems. But, nevertheless, the JavaScript engine performs many of the same steps, albeit in more sophisticated ways than we may commonly be aware, of any traditional language compiler.*", after that, he explained the compiled-language process with some steps. – haitran Nov 03 '14 at 07:49
  • @slebetman So, where should I learn JS theory? – haitran Nov 03 '14 at 07:53
  • @haitran: Unfortunately, in my opinion, no single place. It took me several years working with javascript to develop a feel for the language. It's a nice language once you get to know it. But due to its history not a fully documented language. Even the specs don't really document implementation details such as "is it compiled". Nor the existence of the two phases of execution: compilation and evaluation. Most of us old-school js programmers learned it by writing test programs and running them in different browsers. – slebetman Nov 03 '14 at 07:58

2 Answers2

5

Chrome browser uses V8 engine for compiling Javascript just as other browsers may use Rhino Or SpiderMonkey.

V8 is a JavaScript engine built by Google written in C++. It is used for compiling JS, in both client-side (Google Chrome) and server-side (node.js) applications. In order to obtain speed, V8 translates JavaScript code into more efficient machine code instead of using an interpreter.

V8 compiles JavaScript code into machine code at script execution by implementing a JIT (Just-In-Time) compiler like a lot of modern JavaScript engines such as SpiderMonkey or Rhino (Mozilla) are doing. The main difference with V8 is that it doesn’t produce bytecode or any intermediate code. It just compiles JavaScript on the fly.

Hope this helps!

Arushi Bajpai
  • 91
  • 3
  • 6
2

Well, you can probably get into semantics and terminology differences, but two important points:

  • Javascript (in a web page) is distributed in its source code form (or at least in minimized text form) and not as a binary compiled ahead-of-time

  • Javascript is not compiled into executable machine code even by the browser (although some parts of it may be these days as a performance optimization), but executed via a virtual machine

Thilo
  • 250,062
  • 96
  • 490
  • 643
  • Chrome (V8), Safari (squirrelfish) and IE9+ all have JIT compilers that compile bits of the bytecode to native machine code just like Java. In fact, newer Safari use llvm as it's javascript "interpreter" - llvm is a C/C++/C#/Pascal/Fortran/etc. compiler. – slebetman Nov 03 '14 at 07:43