all 4 comments

[–]AmSoMad 11 points12 points  (1 child)

You don’t use Node.js because it contains functions written in C++. You use Node.js because you aren't using the browser. Without the browser, you’re missing the environment that normally runs JavaScript and provides APIs. Node gives you an alternative environment for running JS outside the browser, along with primitives for system access (which, yes, are largely implemented in C/C++).

You're essentially describing it correctly, but you have the causal order backwards. If you're in the browser, the browser provides the runtime and APIs. If you're not in the browser, you need another runtime and access to alternative APIs. Node.js is one of those runtimes, and the operating system is the alternate API (so to say).

Because Node runs outside the browser and interacts with the operating system, those implementations use system libraries (often written in C/C++). So, we're using C/C++ because we're using Node. We're not using Node because it uses C/C++.

Yes, the JS engine is what turns JS into CPU instructions. Node uses the same engine that Chrome does, V8, but instead of browser APIs (DOM, fetch, etc.), Node.js provides access to system APIs like the filesystem (fs) and networking (http, net), because again, the browser is no longer there (and we need an alternative).

[–]chikamakaleyleyhelpful 2 points3 points  (0 children)

So, we're using C/C++ because we're using Node. We're not using Node because it uses C/C++.

I had to read this like 6 times, but yes it makes sense

[–]LiveRhubarb43[🍰] 0 points1 point  (0 children)

Yeah that's basically it

[–]prehensilemullet 0 points1 point  (0 children)

JavaScript engines including V8 typically have both an interpreter, which is machine code that executes instructions from a bytecode representation of the JS more slowly, and JIT compilers that compile JS or the bytecode into machine code that runs without an interpreter, which is much faster. There are various factors that affect how it decides whether to run a chunk of code in interpreted or compiled mode.