How does JavaScript run on old Browsers? And is it compiled or interpreted?

ยท

3 min read

How does JavaScript run on old Browsers? And is it compiled or interpreted?

Do you ever think about how we write the latest version of JavaScript and still it is compatible with the old browsers? Do browsers interpret or compile code while rendering web applications?

You'll get all these answers after the end of this blog.

Let's start with some definitions:

0EDD0FFB-BCF0-420C-B6FE-FF6B6281D6AD.png

  • Compiler: Compiler is the computer program to convert the code in any language to any other language to make them understandable by any other compiler or computer. Some languages like C, C++ are compiled using compilers like gcc, g++ to machine code which can be easily executed and understood by computers. BEFDCC89-5E68-44FF-9182-D433AAAAB772.png
  • Transpiler: Transpiler is the special case of compiler where the specific version of a language is converted to any other version or any higher-level language to other which is called transpilled. Babel is used to transpile the modern version of JS code to a lower version like ES6 code to ES5 code to make it backwards compatible, converting Typescript to Javascript by TS compiler and JS to lower version of JS using Babel. 41E92E5A-A7FF-48F3-8B0A-E05191FEE71C.png Therefore, the transpiler makes the modern version of JavaScript backward compatible to easily run on the old browsers.

But, when we have some new in-built function in a newer version of the language then how will the previous version of the language understand the same? Here comes the polyfill, which declares the function for the older version of the language. As the name implies, it fills up the missing implementations.

Transpilers convert the syntax of the language and polyfill fills up the in-built function implementation gaps.

Is Javascript compiled or interpreted?

Before moving to any conclusion, let's discuss what is compiled and interpreted.

  • Compiled Language: Compiler takes the code all at once, convert it into the machine code and then machine code is executed. C++, C is a compiled language as the code is converted to the machine code and then executed.
  • Interpreted Language: Interpreter takes every line of code and gives the output without having any intermediate machine code stage. Python is an interpreted language where line by line code can be executed.

Moving on to the conclusion, Javascript is compiled or interpreted according to its implementation by any of the browser engines like V8 by Google Chrome or SpiderMonkey by Mozilla Firefox. If the Javascript is compiled using JIT(Just in time) compiler then it is an interpreted language and if it uses AOT(Ahead of time) compiler then it is a compiled language.

We can discuss in the comments if you have doubts or any improvements to suggest.

Thanks for giving it a read and give ๐Ÿ‘ if you liked it.

You can connect with me on @Saurabh Suthar.

ย