DEV Community

Cover image for Is Javascript a compiled language?
Fabio Russo
Fabio Russo

Posted on

Is Javascript a compiled language?

It really Is...

Why do people still look at JS as a dynamic or interpreted language?

There's a lot of misconception about the compiling of JS, and still now, with lots of information on the web, most people still argue with that and still don't know how exactly JS works before the runtime phase.

Javascript is a compiled language...

despite the fact that the compiling of JS works in a different way, if compared to other compiled language, It's still following some rules that reflect the process of compiling

First... we've to quote this from wikipedia:

A compiler is computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language).

We all knows that computers don't speak Java or JS or Python and don't matter which language we're using, we're always translating our code into something that the machine can understand... but that's not the most important thing for now.

Important is... this kind of translation is called code generation and it's input is the Abstract Syntax Tree (AST) that is about some nested elements that represent the structure of the program. The structuring of this tree, happens in the parsing phase of compiling.

Of course we've to provide something to create this AST ... and we do ... we provide an array of tokens, from the previous compiling lexing phase.

let dog = labrador;

will be tokenized like this

let,dog,=,labrador,;

This splitted version of our code, means something for the language and creates that stream of informations to generate the AST.
We now have a variableDeclaration and an assignment and so on... in our tree.

I've been not very specific, because this post is about the fact that in JS all of this, It's happening.

Yes.

Javascript follows all of this compiling phases, in the order:

  1. Lexing
  2. Parsing
  3. Code Generation

The JS compiling It's not happening to make it works on different platforms or stuff like that... but It's happening

This is not something you should know as generic... this is something that can totally change your point of view about lots of JS behaviours.

Just quick examples are lexical scoping and hoisting.

The variable declarations in JS happens during the lexing phase, while the assignement happens on runtime and that's why hoisting It's happening in a more technical and correct point of view.
The scope, in JS, It's defined in It's lexing phase and that's why JS has got the lexical scoping definition.

What about closures ? More complex... but still something that happens because of scope reference and lexical scoping.

So, people, JS is quickly compiled, everytime... and there's lot of optimization included in the engine to make it possible without any collateral problem in performances, that you can break if you're not conscious about this stuff.

Have fun, looking for more info!

Top comments (22)

Collapse
 
lexlohr profile image
Alex Lohr

Actually, the ability to evaluate JavaScript during run time (new Function(...), eval(...)) means that it cannot ever be a fully compiled language. A JIT compiler as part of the interpreter is pretty common for interpreted languages like JavaScript, Lua or Python nowadays, but doesn't change the fact that the language itself is still interpreted.

Collapse
 
genta profile image
Fabio Russo

The evil eval It's something that changes the behavior of JS "compiling" phase. That's why we should never use It.
Anyway, yes, It's not "totally" a compiled language and not only interpreted.

Collapse
 
lexlohr profile image
Alex Lohr

You can do very much the same by instantiating a new Function() from a string (though you get another scope then, unlike with eval). You can overwrite functions during run time, too.

Anyway, I think your argument is invalid: just because some parts of the code are compiled just in time as an optimization doesn't mean that the language itself is a compiled language. A subset of the language could be a compiled language, but that's another story. Another subset of the language (and partially a superset, but that's a different story) is a data storage format called JSON.

Thread Thread
 
genta profile image
Fabio Russo

I see...

On yesterday, after this post, I was looking around for more info.
I found the "You don't know JS" book, pretty similar in description about this argument:
link to Git Page

It may be self-evident, or it may be surprising, depending on your level of interaction with various languages, but despite the fact that JavaScript falls under the general category of "dynamic" or "interpreted" languages, it is in fact a compiled language.

What's your idea about this? I'm curious, because in many ways, It seems that anyone is correct about this "compiled or not" discussion.

Thread Thread
 
lexlohr profile image
Alex Lohr

There is a gray area in which this discussion happens: virtual machines like .NET or the JVM.

One could make the case that code that runs on such a VM is also compiled, since the "bare metal" on which the actual code runs is just behind a small abstraction layer to allow independence from actual hardware implementation.

But here's the issue I have with the assertion that JS was a compiled language: if your language requires you to start a JIT compiler during run time in certain cases, you're disqualified from calling yourself "a compiled language", because then you go back to interpreter mode.

Granted, that shouldn't happen with most modern JavaScript code, but the language itself is a modern one that is still compatible with all of its bad parts.

Thread Thread
 
genta profile image
Fabio Russo

Granted, that shouldn't happen with most modern JavaScript code, but the language itself is a modern one that is still compatible with all of its bad parts.

Sure, but, there are too many code based on those "bad parts".
By fixing It, you will break lots of stuff all around the web.

Thread Thread
 
lexlohr profile image
Alex Lohr

That's why I refrain from calling JavaScript a compiled language. Please let's not break the web.

Thread Thread
 
qm3ster profile image
Mihail Malo • Edited

Yes, the fact new Function() doesn't capture scope makes it much better optimized, but both are still bad for security (esp in web) and performance.

Can't wait for native DOM access in WASM.

Let's break the damn web already!

Collapse
 
deanchalk profile image
Dean Chalk

Javascript is not a compiled language - period.
A Compiled language is one that when compiled it converts language code into either machine code (to run on the metal - eg c++), or bytecode (to run in a VM - eg Java / C#), and this is done 'Ahead of Time' (AOT), and you deploy the compiled code.
An interpreted language is one where the language code is compiled to machine code or bytecode at the moment of use. the language code is deployed 'as-is' and the interpreter will do the work when the app is running.

When Javascript developers talk about compiling they are really talking about something else - usually tree-shaking and minifying etc. The output of the javascript 'compile' phase is just an optimised string of Javascript code

Collapse
 
genta profile image
Fabio Russo

Thanks for replying mate.

What about that javascript "compile" phase? Just an optimised string?
Can you explain the optimization steps for me?

Collapse
 
sauloxd profile image
Saulo Furuta

I guess the optimization he is mentioning is the minify/uglify of code to reduce the user network cost to "execute"/"run" our client-side web application. In the network point of view, they are just it, a string of chars that will be evaluated in the browser JS engine.

Although I don't actually agree in the usage of "compilation" in this minify/uglify/transpile scenario, because compilation heavily implies in optimization code changes to improve the program runtime, and the steps mentioned only optimizes the network cost/developer UX.

Collapse
 
val_baca profile image
Valentin Baca

Read past the first sentence of wikipedia:

The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program.

You're using the broader definition (what you quoted) vs the colloquial definition (what I've quoted).

Can normal javascript produce a stand-alone executable? Can javascript be run without an interpreter?

That said, there are efforts to actually make js able to be compiled: hackernoon.com/javascript-compilat...

Collapse
 
genta profile image
Fabio Russo • Edited

I wrote that JS is not “compiled” as many other languages but It has some behavior of compiled language before the runtime.
So, I think that we can look at It as a compiled language...

But that’s why this is a #discussion

Collapse
 
lepinekong profile image
lepinekong

There can be many definitions for the same word and there is often one that is widely accepted when context is not precised, the one on Wikipedia is not the implicit one: a language that can be transformed into a binary format close to the machine OS, at least a VM. And connoted with that the language himself is often 2nd generation language with strong typing which facilitates that compilation and so not very forgivable to human unlike non-compiled language. So even if javascript would compile to webassembly, it wouldn't be considered a compiled language from that viewpoint.

Collapse
 
phase profile image
Jadon Fowler

There are no “compiled” or “interpreted” languages. There are only implementations of languages that may use an interpreter or AOT/JIT compiler. JavaScript isn’t a “compiled language” because that title doesn’t make any sense. The specification for your language could recommend that you compile it, but I could write a C interpreter that completely fits the spec.

The way a language is parsed also has nothing to do with how the backend works. Interpreters and compilers both parse the source code (or they might not if they’re really weird).

Collapse
 
bgadrian profile image
Adrian B.G.

If you take it that literal ... yes ... but no.

It is so dynamic that it doesn't even compile a function until is called for the first time.

JS is compiled into machine code, but at run time, is exact the opposite of what people mean when they talk about "compiled languages". Also even if is technically not correct, most of the time we refer to JS compiler engines as "interpretors". It is correct if you consider the dynamic interpretation "eval", or when considering that you need a VM/engine to translate it at each run.

I think it is a good thing that you want to raise awareness about the technical details of what is happening to JS code when is executed, devs will write better code hopefully with this in mind.

PS: if you want to be that literal, you should not use the term JavaScript at all, it is ECMAScript.

Collapse
 
genta profile image
Fabio Russo

I forgot the fact that every block is compiled when called... as you said for the function.

I know that the idea of “compiling” is not fitting totally to It... but It’s there in many ways.

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

Anyway, from what I know, all of these are just implementation details. I think ECMAScript doesn't specify if it should be interpreted, compiled or when each block should be compiled.

Bottom line, I agree that all devs should know more about how the compiler works, a lot of bad code is written under the "it is more optimal this way" umbrella, not knowing that the compiler will do that for him anyway, and they could have kept the code more human friendly.

Thread Thread
 
genta profile image
Fabio Russo

Agree.

JS is pretty easy at a first sight, but if you want to use It not just as the “browser languagae” you need to know grammar, lexing, values, coercion...
Non that It’s needed to know the specifications completely (they’re always online for you) but at least, one time, you should read It and have an idea on how It works.

Collapse
 
goomatyi profile image
Goodwish Matyila

I know this may sound strange to many self taught javascript coders, but the truth shall set you free. JavaScript is a compiled language from what i learned and many others using ES5 Strict mode and Scala types.

This is valid javascript code:

var age : int = 31;

For many people on the who studied .NET Framework understand that jsc is CLI compiler for .NET specifically for JScript. which uses proper namespaces, classes etc... but also JScript with Rhino compiles to .class files for Java language.

JavaScript runs on the browser, server, .NET framework, cloud ,nodejs its everywhere.... I still use javascript to write webservices and compile to .dll everytime.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.