Is Javascript a compiled language?
Tags: discuss, javascript, compile
Published: 2018-05-16 09:26:21

---
title: Is Javascript a compiled language?
published: true
description: wondering why...
tags: discussion, javascript, compile
cover_image: https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fd-zKJCKsoWw%2Fmaxresdefault.jpg&f=1
---

#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.

<center>**Have fun, looking for more info!**</center>