Currently, JavaScript is a general-purpose language. That is, it can be executed in a wide variety of environments.
Traditionally, JavaScript has been closely associated with web development (and still is). But today we can use it in many other fields.
To execute JavaScript, we only need an interpreter that is capable of executing the code.
The interpreter (or runtime) is a program that reads the source code and executes it directly, line by line.
JavaScript in Web Browsers
Since its inception, one of its main uses has been adding interactivity to web pages. In this context, the web browser itself acts as the interpreter of JavaScript.
When a web page is loaded, the browser (Chrome, Firefox, Safari, etc) receives the JavaScript code as part of the web content.
The JavaScript engines built into browsers (like V8 in Google Chrome, SpiderMonkey in Firefox, and JavaScriptCore in Safari) are responsible for interpreting and executing this code.
JavaScript engines have evolved tremendously since the early days of the language. They now incorporate advanced techniques that greatly enhance performance (such as Just-In-Time compilation).
Executing JavaScript in Other Environments
Although the web browser remains a very important environment for executing JavaScript, over time, other runtimes have emerged that allow JavaScript to be executed outside the browser.
This is referred to as running JavaScript on the server. But the server can be anything, including your personal computer. What it means is outside the browser.
Node.js
The first and most famous of these runtimes is Node.js, which was introduced in 2009. Node.js allows executing JavaScript on the server, using Google Chrome’s V8 engine.
At the time, this was a revolution, as it allowed developers to use JavaScript on both the frontend and the backend, benefiting from its asynchronous and event-driven programming model.
Other Runtimes
In recent years, other runtimes have emerged that seek to improve and expand the capabilities of Node.js.
Deno: Created by Ryan Dahl, the same creator of Node.js, Deno was released in 2020 with the aim of correcting some design flaws of Node.js. Deno provides a secure environment by default, with explicit permissions for file access, network, and other system resources. Additionally, it natively supports TypeScript and has a module system based on URLs.
Bun: It is an emerging runtime designed to be extremely fast, focusing on efficiency and simplicity. Like Deno and Node.js, Bun uses the V8 JavaScript engine but introduces additional optimizations to enhance the performance of JavaScript applications.
These are some of the most well-known, but there are many runtimes, including some very lightweight ones to run on the “edge”.
Other Execution Environments
JavaScript has become so ubiquitous that it can now be found in a wide variety of environments, including:
Desktop Applications
Using frameworks like Electron, it is possible to build desktop applications with web technologies (HTML, CSS, JavaScript).
Electron is based on Node.js and Chromium, allowing developers to create cross-platform applications.
Mobile Applications
Frameworks like React Native allow for the development of native mobile applications using JavaScript.
React Native translates JavaScript code into native components, providing a user experience similar to that of applications written in Swift or Kotlin.
Automation
Tools like Puppeteer and Selenium use JavaScript to automate web browsing tasks and application testing, which is very useful in software development and maintenance.
JavaScript on Hardware Devices
But JavaScript is not limited to traditional software environments. We can also execute it on hardware devices like Arduino and ESP32 (which we really like here, you already know ❤️)
For example, Johnny-Five is a JavaScript framework that allows programming embedded systems using JavaScript. (for instance, we can control devices like LEDs, sensors, motors, and other components).
In short, you can use JavaScript in many places. It is no longer just a simple scripting language for web browsers.
Today, it is a genuinely general-purpose programming language that can be executed in a wide variety of environments, from servers to hardware devices and desktop and mobile applications.