Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JavaScript compiled to machine code when executed in a Web Browser Environment?

Tags:

javascript

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:

  • JavaScript Engine - Wiki
  • How the V8 Engine Works
  • How do Browsers Handle JavaScript
like image 462
HelloWorld Avatar asked Oct 10 '16 22:10

HelloWorld


People also ask

How does JavaScript code is executed in browser?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.

How is JavaScript translated to machine code?

In order to obtain speed, V8 translates JavaScript code into more efficient machine code instead of using an interpreter. It compiles JavaScript code into machine code at execution by implementing a JIT (Just-In-Time) compiler like a lot of modern JavaScript engines do such as SpiderMonkey or Rhino (Mozilla).

Is JavaScript executed by browser or server?

Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine. The browser has an embedded engine sometimes called a “JavaScript virtual machine”.

Is JavaScript compiled or interpreted?

What is JavaScript? JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it's used in many non-browser environments as well.


1 Answers

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.

like image 168
Rodrigo5244 Avatar answered Oct 08 '22 15:10

Rodrigo5244