21

I'm trying to get a better understanding of how JavaScript is executed in a web browser environment.

In terms of Node.js, I understand that the JavaScript code written in a Node.JS program is compiled with C++ code (V8), and ultimately becomes machine code. Since Node.js can interact with the filesystem and other machine level tasks, to me it makes sense why it has to eventually become machine code.

I feel differently about the web browser environment. From my understanding, the main goal is to interact with the DOM. Does JavaScript need to be compiled into machine code to just interact with the DOM?

I'm puzzled by this. Node.js and Chrome both run on V8. V8 is written in C++ and to my knowledge, compiles JavaScript code into machine code a processor can understand.

You need a JavaScript engine to implement ECMA-262, that is the whole purpose of an engine (I think?). But, does a web browser need JavaScript to be compiled to a Machine Language Level, what machine operations is it performing?

Here are a few articles I've researched, unfortunately, I haven't found an answer to my question in them:

19
  • I don't have a full answer to your question, but this is going to depend on the browser at the very least, and what js engine it uses. Most the big players have their own unique engines or at least their own forks of existing engines. Commented Oct 10, 2016 at 22:50
  • 1
    Who knows? Maybe it is, maybe it isn't. Modern JavaScript runtimes may indeed compile small pieces of code into native binary form. There's really no way your program can tell. Why does it matter? Commented Oct 10, 2016 at 22:51
  • And yes, as Jaromanda X says, you're completely wrong about how Node works. Commented Oct 10, 2016 at 22:51
  • 2
    The second link in the question contains the following: It compiles JavaScript code into machine code at execution by implementing a JIT (Just-In-Time) compiler ... The main difference with V8 is that it doesn’t produce bytecode or any intermediate code. Commented Oct 10, 2016 at 23:08
  • 1
    @LenilsondeCastro nitro, spidermonkey, chakra and v8 do. Commented Oct 10, 2016 at 23:58

1 Answer 1

10

The engine is written in C++, then this code is translated into machine code by a compiler. Once the code is in machine language, it can be run by the computer. While the engine is running, it can read code written in JavaScript, interpret it, and execute what the code is asking it to do. In this case, what is actually running in the computer is the engine code, that just happens to be doing what another code is telling it to do. The difference between node and a browser is that the browser won't do anything that a JavaScript is asking it to do. Another thing to keep in mind is that some browsers and node translates JavaScript code to machine code in real time to get more speed. Browsers are also careful not to write machine code that is dangerous, but in theory that could happen.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.