Javascript Engine

 Interpreted vs Compiled

  • js is considered an interpreted language (not compiled). 
  • there is no executable to be deployed such as a jar file and each line is read and then processed and executed 
  • but a lot is going on under the hood when javascript code is processed by the javascript engine some of which is very "compiler like"


Let's look at this code example. The 3rd line is an error but if code was truly interpreted then would see the console.log() and then error. But that does not happen, we see the error without any log. Why?

Example 1:

let sayHi = 'hello';

console.log(sayHi)

sayHi = ."hi";



Let's look at another example.....same thing we see the error before any log. Why?

Example 2:

console.log("Howdy");

saySomething("Hello","Hi");

function saySomething(greeting,greeting) {

    //"use strict";

    console.log(greeting);

}


At a high level, the javascript engine is making multiple passes over the code, not just one. It all happens very quickly, in milli seconds.

Can consider javascript code is processed in 2 main phases

  1. parsing/compilation ...where in effect the code is being compiled i.e. processed and analyzed 

  2. execution

So javascript is not simply interpreted.

Within parsing/compilation are more steps:

  1.  tokenizing - breaking up the strings of code
  2.  parsing    - creating an Abstract Syntax tree
  3.  code generation - generating executable code




Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more