Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 adynamic orinterpreted language?

There's a lot of misconception about thecompiling 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 theruntime phase.

Javascript is a compiled language...

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

First... we've to quote this fromwikipedia:

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 computersdon't speak Java or JS or Python and don't matter which language we're using, we're alwaystranslating 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 calledcode generation and it's input is theAbstract Syntax Tree (AST) that is about somenested elements that represent the structure of the program. The structuring of this tree, happens in theparsing phase of compiling.

Of course we've to provide something to create thisAST ... and we do ... we provide anarray of tokens, from the previous compilinglexing phase.

let dog = labrador;

will betokenized like this

let,dog,=,labrador,;

Thissplitted version of our code, means something for the language and creates that stream of informations to generate theAST.
We now have avariableDeclaration and anassignment and so on... in ourtree.

I've beennot very specific, because this post is about the fact thatin JS all of this, It's happening.

Yes.

Javascript follows all of thiscompiling phases, in the order:

  1. Lexing
  2. Parsing
  3. Code Generation

The JScompiling It's not happening to make it works on different platforms or stuff like that... butIt's happening

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

Just quick examples arelexical scoping andhoisting.

The variable declarations in JS happens during thelexing phase, while the assignement happenson runtime and that's whyhoisting It's happening in a more technical and correct point of view.
The scope, in JS, It's defined in It'slexing phase and that's why JS has got thelexical scoping definition.

What aboutclosures ? More complex... but still something that happens because ofscope reference andlexical 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
lexlohr profile image
Alex Lohr
...former musician, voice actor, martial artist, started coding 38 years ago and turned front-end developer 25+ years ago.

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.

CollapseExpand
 
genta profile image
Fabio Russo
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)

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

CollapseExpand
 
lexlohr profile image
Alex Lohr
...former musician, voice actor, martial artist, started coding 38 years ago and turned front-end developer 25+ years ago.

You can do very much the same by instantiating anew 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
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)

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
...former musician, voice actor, martial artist, started coding 38 years ago and turned front-end developer 25+ years ago.

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 thelanguage itself is a modern one that is still compatible with all of its bad parts.

Thread Thread
 
genta profile image
Fabio Russo
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)

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
...former musician, voice actor, martial artist, started coding 38 years ago and turned front-end developer 25+ years ago.

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
  • Joined
• Edited on• Edited

Yes, the factnew 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!

CollapseExpand
 
val_baca profile image
Valentin Baca
Sr. Software Engineer & Tech Lead. 10+ years of experience, primarily at Amazon.com. Currently at Relief.appMy opinions are mine.
  • Location
    Seattle, WA
  • Education
    B.S Computer Science & B.S. Electrical Engineering from Texas Tech University
  • Work
    Tech Lead at Relief.app
  • Joined

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

CollapseExpand
 
genta profile image
Fabio Russo
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)
• Edited on• 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

CollapseExpand
 
deanchalk profile image
Dean Chalk
  • Joined

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

CollapseExpand
 
genta profile image
Fabio Russo
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)

Thanks for replying mate.

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

CollapseExpand
 
sauloxd profile image
Saulo Furuta
🍊
  • Email
  • Location
    São Paulo
  • Work
    Frontend developer at QultureRocks
  • Joined

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.

CollapseExpand
 
phase profile image
Jadon Fowler
  • Joined

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

CollapseExpand
 
bgadrian profile image
Adrian B.G.
Striving to become a master Go/Cloud developer; Father 👨‍👧‍👦; 🤖/((Full Stack Web|Unity3D) + Developer)/g; Science supporter 👩‍🔬; https://coder.today
  • Email
  • Location
    E EU
  • Education
    Bachelor of Economic Informatics
  • Work
    Cloud Engineer at CrowdStrike
  • Joined

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.

CollapseExpand
 
genta profile image
Fabio Russo
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)

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.

CollapseExpand
 
bgadrian profile image
Adrian B.G.
Striving to become a master Go/Cloud developer; Father 👨‍👧‍👦; 🤖/((Full Stack Web|Unity3D) + Developer)/g; Science supporter 👩‍🔬; https://coder.today
  • Email
  • Location
    E EU
  • Education
    Bachelor of Economic Informatics
  • Work
    Cloud Engineer at CrowdStrike
  • Joined
• Edited on• 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 ofbad 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
Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)

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.

CollapseExpand
 
lepinekong profile image
lepinekong
  • Joined

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.

CollapseExpand
 
goomatyi profile image
Goodwish Matyila
"I help companies build platforms, cutting back slow feedback on projects, by accelarating reviews and productive output geared to boost sales, IT infrastructure"-- CEO G Matyila
  • Location
    South Africa
  • Education
    IT Engineer (Educon), IBM ML, AI, SEO (UC Davis), Responsive Web Design(University of London)
  • Work
    CEO at BD Group (Pty) LTD.
  • Joined

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.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Passionate with code... In love with Web Design, Videogames, Books and Jazz music.Years of experience in "document composition" and a Front-End Developer wannabe (since years)
  • Location
    Bergamo
  • Work
    FrontEnd Developer
  • Joined

More fromFabio Russo

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp