What Does JavaScript Do Under the Hood?

If you want to be a web developer, there are three basic elements you need to learn: HTML, CSS and JavaScript. These form the foundation of a website or web application. They provide the structure (HTML) and the aesthetic feel (CSS). JavaScript is the magic third element that brings everything to life. It makes websites dynamic and allows you to submit forms, click links and interact with the content. But what exactly does javascript do under the hood?

JS is a scripting language that was developed in 1995 at Netscape to enable interactivity on webpages. It can do client-side scripting or communicate with servers asynchronously. As a client-side language, it lets developers manipulate the Document Object Model (DOM) and handle user events. It can also perform tasks like validation and form submission.

It’s a multi-purpose, cross-platform, scripting language. The syntax of javascript is heavily inspired by languages such as C++ and Java. But its inner workings are closer to a dynamically typed, interpreted language.

As you write a script, the compiler checks your code to make sure it’s valid. It then runs it through an engine that converts it to machine code. The engine then executes the code line by line. During the execution process, the engine keeps track of all function calls using a LIFO data structure called a call stack. As a function is invoked, the engine pushes a new ‘Stack Frame’ to the call stack, storing its arguments and local variables. The engine then uses the return address in the Stack Frame to resume execution at its next return point.

The js engine also employs some best practices to ensure efficient execution. It does this by minimizing the amount of interaction with the DOM and reducing the number of function calls. Frequent DOM manipulations and excessive function calls can cause performance lag.

Inline functions reduce the number of function calls and improve performance. Inline functions are a good way to wrap long, complex functions and to avoid having to pass them as parameters. They can also be used to add code to the DOM.

JavaScript can also do math and other arithmetic operations on data types such as integers, booleans, floats and strings. It supports a set of 63 arithmetic operators and has many other mathematical functions as well. It also supports a variety of logic operations such as comparisons and logical functions. In addition to these, javascript provides constructs for looping and conditional execution.