Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Compiled language?

Tags:

javascript

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?

like image 770
haitran Avatar asked Nov 03 '14 07:11

haitran


2 Answers

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!

like image 69
Arushi Bajpai Avatar answered Oct 08 '22 00:10

Arushi Bajpai


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

like image 39
Thilo Avatar answered Oct 07 '22 23:10

Thilo