I was going through the docs and couple of answers here n there. I noticed that V8 compiles our code into machine code. So, is Node.js interpreted or compiled?
It's interpreted and then compiled on the fly using JIT, so somewhere in the middle.
Unlike Java which compiles to byte code and then the byte code is compiled on the fly to something the machine can understand, NodeJS stays JavaScript and then the JavaScript is compiled on the fly.
Node.js is not a language. It is a runtime environment for Javascript, much like web browsers are. Both node.js and web browsers use V8, which is a javascript "engine", which means it's the part of the code that parses and executes Javascript code. V8 happens to use a JIT execution model. Node itself is not specific to V8 (anymore) and can embed on other JS engines as well, including engines that are pure interpreters, like most were before V8, although all popular engines in use today use JIT compilation.
Interpreted. It might be a bit confusing because of the es6 to es5 shimming.
Denny Trebbin
Lead Fullstack Developer. Experimenting with bleeding-edge tech. Irregularly DJ. Hobby drone pilot. Amateur photographer.
It's both and more.
V8 is a VirtualMachine basically which has an interpreter and multiple compilers and optimizers.
The JS code will be compiled directly into machine code. An optimizer is trying to find the most aggressive optimization(s). In each of those steps, the output gets stored in a cache. The code will execute and on failure, a previous optimized or compiled code will be executed from the cache instead. V8 can re-order steps when certain code fragments are identified and known to perform well. The compiler and optimizer are guarded by type checkers to pick to most efficient type. Int8 for small numbers for example, not the wasting doubles. Only in the case of all cache steps can't be executed and/or the types of variables can't be determined then the code gets interpreted.
Edit: JS engines like V8 (Node.js) SpiderMonkey and ChakraCore are interpreters (for compatibility reasons), compilers (JIT and AOT), optimizers. V8 and SpiderMonkey have pre-compiled machine code (AOT) to replace identified and known fractions of code with high-performance machine code after AST creation and before even pushing it to JIT compiler. Such AOT code is for loops and some expressions. Optimizers can inline functions like it's known from C compilers also done after AST and before pushing to JIT.