DEV Community

Cover image for How Computers Understand JavaScript Code
NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on

How Computers Understand JavaScript Code

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 shows that the following code will output "Hello World."

01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100
Enter fullscreen mode Exit fullscreen mode

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

console.log("Hello World.")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

instead of

10111000
Enter fullscreen mode Exit fullscreen mode

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:

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

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 known as the V8 Engine. 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

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.

How JavaScript code can run on the Server-Side

Nodejs is written in C++ and wraps the V8 Engine 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

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 for details on how to create server-side applications and this MDN documentation to learn the fundamentals of JavaScript.

Oldest comments (0)