Is Javascript a compiled language?

Fabio Russo - May 16 '18 - - Dev Community

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!
. . . . . . . . . .
Terabox Video Player