What actually is JavaScript?
Tags: javascript, webdev, beginners, programming
Published: 2022-09-05 06:20:05

_To build great software we should have deep enough understanding of the internals._ There are so many developers who don't have any idea of what is going on “under the covers”.
So in this article ill try my best to explain the internal working of JavaScript in simplest way.

> **Before we begin, here is a fact for you:**
> #####Did you know JavaScript supports a modern touchscreen-based UI for the SpaceX Dragon spacecraft?
> Yes JavaScript was actually used to build UI inside SpaceX spacecraft.

![JavaScript Runtime Environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhgcm2xlgta6pci1ecvl.png)
 

JavaScript works on a environment called **JavaScript Runtime Environment**. To use JavaScript you basically install this environment and than you can simply use JavaScript.
So in order to use JavaScript you install a **Browser** or **NodeJS** both of which are JavaScript Runtime Environment.

Depending on the context, the runtime environment can take on different forms; for example, the runtime environment in a browser is very different from that of Node.js. Because these differences are primarily at the implementation level, the majority of the following concepts remain applicable.

![JS Engine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw8i0g80lxm3sji3rtl1.png)


**JavaScript Engine** is a part of JavaScript Runtime Environment and each web browser has its own version of the JS engine.
One thing to note here is that JavaScript can be used to write only logic like for ex: for loops, while loops, if-else etc.
Example, **Chrome** uses the **V8 JS engine** which has been developed by the Chromium Project. **Firefox** uses **SpiderMonkey** which was first written by **Brendan Eich** at **Netscape** and is now maintained by the folks at **Mozilla**. Apple's **Safari** uses **Webkit**.

The purpose of the JavaScript engine is to translate source code that developers write into machine code that allows a computer to perform specific tasks.
JS Engine is made up of the **heap** and the **call stack**

**The heap**, also called the ‘memory heap’, is a section of unstructured memory that is used for the allocation of objects and variables.

**The call stack** is a data structure that tracks where we are in the programme and operates on a last-in, first-out basis. Each entry in the stack is referred to as a stack frame. This means that the engine is focused on the frame at the top of the stack, and it will not move on to the next function unless the function above it is removed from the stack.

As the JS engine steps into a function, it is pushed onto the stack. When a function returns a value or gets sent to the **Web APIs**, it is **popped off** the stack. If a function doesn’t explicitly return a value then the engine will return undefined and also pop the function off the stack. This is what is meant by the term “JavaScript runs synchronously”; it is single-threaded, so can only do one thing at a time.

JavaScript Runtime Environment also provide **APIs** which are not a part of JS Engine. We have different APIs for different runtime environment. So features like **console.log** and **DOM** are not provided by JavaScript but by the APIs of browsers runtime environment and same for **setTimeout()** this method is not provide by JS but NodeJS runtime environmet.
So JavaScript can be used to write only logic with JS Engine but with help of APIs we have a lot more features.

**Electron** is also a runtime environment used for creating Desktop Applications

This is the reason you never install JavaScript remotely like **Java** or **Python** in your Desktop because it comes pre installed with Browser or NodeJS or other JS Runtime Environment.

######Thanks for reading this article, follow for more