You might have wondered whether javascript is a compiled or interpreted language ๐ค
But you know what? it's neither just compiled nor just interpreted ๐คฏ
Here's why ๐งต๐
In this thread I will explain why javascript is not considered neither just compiled nor just interpreted.
So first of all let's discuss what are complied and interpreted langauges.
1/ What are complied languages?
Code written in compiled languages like c and go is directly converted into binary, which can be directly executed by the processors.
This generally results in faster and more optimised code as compared to interpreted languages.
2/ What are interpreted languages?
In interpreted languages like python, code is not directly compiled to binary, but instead is executed by another program i.e interpreter.
This generally results in slower execution as compared to compiled languages.
3/ Now, where does javascript fits in?
First of all, how javascript is executed totally depends on the runtime environment, for eg v8, spider monkey, etc.
But most of the engines compile and run javascript just at runtime, it is also known as "just in time (JIT) compiling".
4/ What is JIT?
JIT compiling is a mix of both interpreted and complied, it compiles the code to the native code at the runtime itself, so best of both the worlds.
Engines like v8 which is used by node, deno and chromium use jit compiling to speed up the execution.
5/ So code written in javascript is directly compiled to native code at runtime, now we can't say it is just compiled or just interpreted.
One thing to note here is, that v8 directly converts javascript to native code whereas engines like spider monkey first interprets to byte code and then compiles, like java.
6/ Resources to learn more
These are the resources that made me curious and got me to the answer for this question:
- Forum (MUST read): tinyurl .com/is-js-compiled
- Video by hitesh choudhary: youtu.be/-1rEsrEau7I
That's it for this thread and I hope you've found it helpful!
I help you become a better web developer by sharing helpful resources and demystifying complex topics.
If this is something that interests you, make sure to follow @vedantnn7.
Thanks for checking this out ๐