November 11, 2024

How does JavaScript work?

Laptop

All programming languages work by translating English syntax into machine code, which is then executed by the operating system. JavaScript can broadly be categorized as a scripting or interpreted language. JavaScript code is interpreted, that is, directly translated into machine language code by the JavaScript engine. In other programming languages the compiler processes all the code into machine code in a separate step. Thus, all scripting languages are programming languages, but not all programming languages are scripting languages.

JavaScript engine

A JavaScript engine is a computer program that executes JavaScript code. The first JavaScript engines were just interpreters, but all modern engines use JIT compilation or run-time compilation to improve performance.

Client-side JavaScript

Client-side JavaScript refers to the way JavaScript works in your browser. In this case, the JavaScript engine is inside the browser code. All major web browsers have their own built-in JavaScript engines.

Web application developers write JavaScript code with different functions associated with different events, such as clicking or hovering. These functions make changes to HTML and CSS.

Below is an overview of how JavaScript works on the client side

  1. The browser loads the web page when you visit it.
  2. As it loads, the browser converts the page and all of its elements, such as buttons, shortcuts, and dropdown fields, into a data structure called a document object model (DOM).
  3. The browser’s JavaScript engine converts JavaScript code into bytecode. This code mediates between the JavaScript syntax and the machine.
  4. Various events, such as a mouse click on a button, cause the associated block of JavaScript code to be executed. The engine then interprets the bytecode and makes changes to the DOM.
  5. The browser displays the new DOM.

Server-side JavaScript

Server-side JavaScript refers to the use of a coding language in internal server logic. In this case the JavaScript engine resides directly on the server. A server-side JavaScript function can access a database, perform various logical operations, and respond to various events triggered by the server’s operating system. The main advantage of server-side scripting is that you can largely customize the site response according to your requirements, access rights, and site information requests.

Fragment of program code on a computer screen

Client-side and server-side

Server-side JavaScript refers to the use of a coding language in back-end server logic. In this case, the JavaScript engine resides directly on the server. A server-side JavaScript function can access a database, perform various logical operations, and respond to various events triggered by the server’s operating system. The main advantage of server-side scripting is that you can largely customize the site response according to your requirements, access rights, and site information requests.

Client-side and server-side

The word dynamic describes both client-side and server-side JavaScript. Dynamic behavior is the ability to update the display of a Web page to generate new content as needed. The difference between client-side and server-side JavaScript is the generation of new content.

Server-side code dynamically generates new content using application logic and changing data from the database. Client-side JavaScript, on the other hand, dynamically generates new content inside the browser, using user interface logic and changing the content of a web page that is already on the client side. The meaning is slightly different in these two contexts, but they are related, and both approaches work together to improve the user experience.

The other difference between the two uses of JavaScript, aside from implementing dynamic functions, is the resources the code can access. On the client side, the browser controls the JavaScript execution environment. The code can only access resources that the browser allows it to access. For example, it cannot write content to your hard drive unless you click the download button. On the other hand, server-side functions can access all of the server machine’s resources as needed.