JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.
- The above paragraph states that JS is an interpreted language, not compiled. And later, winks at us and says, "but it uses Just in Time compilation, which compiles JS to executable bytecode to run ".
- Before we label JS as a hypocrite, let's understand what JIT compilation is.
console.log("Hello"); console.log(aVariable); var aVariable = 10; console.log(aVariable); console.log(aConstant); const aConstant = "constant"; /*OUTPUT: VM374:1 Hello VM374:2 undefined VM374:4 10 VM374:5 Uncaught ReferenceError: Cannot access 'aConstant' before initialization at <anonymous>:5:13*/
- If it were a purely interpreted language, it should have given error instead of undefined for line 2 and not known that aConstant is initialized ever down the line.
- JIT compilation. Simply put, it compiles when it starts execution not before hand. JIT mozilla
- Is JS interpreted or compiled?
More reading: Blog - How does JS really work?