Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptIs JavaScript Interpreted or Compiled ?

Is JavaScript Interpreted or Compiled ?

JavaScript is interpreted language. Here we will try to clear the age-long confusion related to JavaScript. One of the biggest questions that arise while learning JavaScript is whether it is compiled or interpreted or uses a JIT(Just In Time) compiler. To understand this concept we should be clear about the definitions of interpreter, compiler, and JIT compiler.

  • Interpreter: An Interpreter directly executes instructions written in a programming or scripting language without previously converting them to an object code or machine code.
  • Compiler: A compiler takes an entire program and converts it into object code which is typically stored in a file. The object code is also referred as binary code and can be directly executed by the machine after linking. 
  • JIT compiler: A JIT compiler first converts the whole code to byte code and then uses the compiler at runtime to convert the code into machine-readable code. JIT compiler helps in faster execution.

How is JavaScript code executed?

The parsing of JavaScript code is done before execution so it looks like a parsed language but the code is converted to binary form before execution. To further understand this concept let us see how this code execution takes place behind the scenes.

  • First, the code is transpiled using babel or any other web pack.
  • This form of code is given to the Engine which converts it to AST(Abstract Syntax Tree).
  • This AST is then converted to the byte code which is understood by the machine. This is an Intermediate Representation(IR) which is further optimized by the JIT compiler.
  • After the optimization, the JS Virtual Machine Executes the code.

Hence we can conclude that JS code is executed in three phases.

Parsing -> Compiling -> Executing

Hence, we can conclude that JavaScript code is initially interpreted before any execution begins

Example: To support this statement we will look at the code example below.

Javascript




console.log("Hello");
for(var i = 0;i<4;i++) {
    console.log(hello);
}


Output: We can see the interpreter behavior in the above code example, here first Hello is printed at the console and then an error is reported. This output strongly supports the fact that JavaScript is an interpreted language.

 

In general it may look as if JavaScript code is being executed line by line because of the parsing phase but the whole code is compiled at once to convert it to machine-readable code before execution. This shows that JavaScript is a Just-In-Time compiled language that uses an interpreter in its first phase.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments