How Computers Understand JavaScript Code
Tags: javascript, beginners, webdev, tutorial
Published: 2023-01-30 06:53:28

Programming has gotten more accessible in recent times due to the increment in the number of high-level programming languages (JavaScript, Python, and so on). 

But It was not always like this. Programmers could only program in Machine language in the past because that is the only language computers understand.

So what changed? Do computers now understand other languages aside from machine language?

I will provide answers to these questions and more in this article. Keep reading…

## Definition of Terms

### Machine Language
This is also called machine code or object code. It is made of binary digits (zeroes (0's) and ones(1's)) and is only what computers understand. They send specific instructions to the CPU to act in one way or another.

As you can imagine, it will be a lot of work to write code with it. For example, [this article](https://www.computerhope.com/jargon/m/machlang.htm) shows that the following code will output "Hello World."

```
01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100
```

You will need a lot of mathematical calculations to decode the program above compared to the JavaScript code below:

```javascript
console.log("Hello World.")
```
### Mnemonics
These are abbreviations that correspond to different machine code instructions. Examples include `MOV`, `ADD`, `SUB`, `FWD`, etc. So to move items from one memory location to another can now look like this:

```
mov DS, 5
```

instead of

```
10111000
```

The code above tells the computer to move `5` into the `DS` memory.

### Assembly Language
This language is also referred to as assembler language or symbolic machine code. It was coined due to the difficulty in programming with machine code. It provides some level of abstraction by using Mnemonics and is converted to machine code using an Assembler (compiler). 

Here is a sample Assembly program to display “Hello World” following [this article](https://www.computerhope.com/jargon/a/al.htm):

```
        global  _main
        extern  _printf
        section .text
    _main:
        push    message
        call    _printf
        add     esp, 4
        ret
    message:
        db  'Hello, World!', 10, 0
```

### Mid-level Language
A language that falls into this category provides a high-level abstraction so that a programmer can write code in a more human-friendly language (such as English), and manipulate low-level code.

In other words, a programmer can tell the computer *what to do* and *how to do it* using the language.

Examples of such programming languages are C, C++, and Rust. These languages are converted to machine code using various compilers and interpreters.

### High-Level Language
A high-level language allows the programmer to write code in a human-friendly language (such as English) without having to bother about how it will be executed. So developers can just tell the computer *what to do* and the computer will know *how to do it*.

This language includes JavaScript, Python, Java, and PHP


## How Do Computers Understand JavaScript?

From the definition of terms above, you already have an idea of how computers understand code written in JavaScript. However, I will provide more context in this section for you to get a clearer picture.

JavaScript is not designed to run on a computer. It is designed to run on a browser. This is why it has been the go-to language for building interactive client-side (Frontend) applications, until recent times when it can now run on the server-side (Backend).

### How are browsers able to understand JavaScript code?
Browsers like chrome can decode JavaScript using a compiler created by [Google](https://www.google.com/) known as the [V8 Engine](https://v8.dev/). This compiler is written using C++ (a mid-level language) and converts JavaScript for the browsers’ understanding.

![How Browsers convert Javascript using the V8 engine](https://paper-attachments.dropboxusercontent.com/s_D592C23061BDAFBA9E611AEDC8048F685A5679FCF2C57746CD5AE80A3DAD15B0_1674733960743_Screenshot+2023-01-26+at+12.34.55.png)

### How is JavaScript code able to run on the Server-Side?
Recent decades have seen a lot of transformation from what JavaScript was initially designed to do (client-side Applications). It is now used for building server-side applications using [Nodejs](https://nodejs.org/en/).

![How JavaScript code can run on the Server-Side](https://paper-attachments.dropboxusercontent.com/s_D592C23061BDAFBA9E611AEDC8048F685A5679FCF2C57746CD5AE80A3DAD15B0_1674733974794_Screenshot+2023-01-26+at+12.34.55+copy.png)

Nodejs is written in C++ and wraps the [V8 Engine](https://v8.dev/) within it. This makes it possible for JavaScript to run directly on the computer without a browser.

![How JavaScript code is converted to machine language on the Server-Side](https://paper-attachments.dropboxusercontent.com/s_D592C23061BDAFBA9E611AEDC8048F685A5679FCF2C57746CD5AE80A3DAD15B0_1674733991719_Screenshot+2023-01-26+at+12.33.31.png)

And that is how it works!

## Conclusion 
This article is aimed at helping you get a clear picture of how computers can understand JavaScript code. 

We were able to see the different layers of languages that programmers can use to talk to the computer and how they are linked. Finally, we saw how JavaScript is converted to machine code.

So do computers now understand other languages aside from machine language? - _NO!_

What changed? - _Computers now have softwares (assembler, compilers, and interpreters) that convert other programming languages to machine code._

How do computers understand JavaScript code? - _Computers use the V8 engine to convert JavaScript code to machine code._

Do check out the [Nodejs documentation](https://nodejs.org/en/docs/) for details on how to create server-side applications and this [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript) to learn the fundamentals of JavaScript.