JavaScript Host Environments

Updated May 17, 2023#javascript#runtime#guides

JavaScript was initially a browser-only scripting language. Today, JavaScript is a widely-adopted general-purpose multi-paradigm programming language that runs literally everywhere (web browsers, web servers, mobile apps, desktop apps, IoT devices).

It is very common for ECMAScript and JavaScript to be used interchangeably. ECMAScript is a specification programming language defined in the document ECMA-262. JavaScript is a dialect of ECMAScript, conforms to the ECMAScript specification while also providing additional features not described in the specification.

Host environments are platforms where JavaScript can run, each of them implements JavaScript language specification and provides additional platform specific functionality.

JavaScript language itself only defines APIs for working with text, dates, regular expressions, standard data structures. Any I/O (networking, storage, graphics, files, etc.) will be optionally provided by host environments.

Browser environments

A web browser provides a host environment for client-side computation including, for instance, objects that represent windows, menus, pop-ups, dialog boxes, text areas, anchors, frames, history, cookies, input, and output.

Web browser also provides a means to attach scripting code to events such as change of focus, page and image loading, unloading, error and abort, selection, form submission, and mouse actions.

The World Wide Web Consortium (W3C) has established standards for Structural and Semantic Languages (HTML, XHTML, XML), Presentation Languages (CSS), Object Models (DOM), Scripting Languages (ECMAScript), Additional Markup Languages (MathML), commonly used to develop web pages.

All modern web browsers (Chrome, Safari, Firefox, Edge) are considered to be W3C-compliant, in that they conform to the set-forth standards and should deliver a consistent web experience across browsers.

JavaScript engines are typically developed by web browser vendors, and every major browser has one. The use of JavaScript engines is not limited to browsers. For example, the Chrome V8 engine is a core component of the popular Node.js and Deno runtime systems.

Non-browser environments

Host environments outside browsers come in different shapes and sizes, relies on popular JavaScript engines (V8, JavaScriptCore, etc.) to handle JavaScript language, provides addition platform-specific features as JavaScript frameworks, and not standardized by any specifications.

JavaScript in server-side has exploded in recent years with the help of Node.js, Deno, and Bun runtime. They provide server-side computation including objects representing requests, clients, files, mechanisms to lock and share data.

  • Node.js (95k ⭐) - A server-side JavaScript environment based on Google’s V8 JavasScript engine, created by Ryan Dahl in 2009 and was heavily focused on event-driven HTTP servers. It brought server-side JavaScript to the mainstream, under development for over a decade, very stable and battle-tested.

  • Deno (89k ⭐) - A modern JavaScript and TypeScript runtime built on the V8 JavaScript engine and Rust programming language. It was created by Ryan Dahl, the original creator of Node.js, with the aim of addressing some of the limitations and security concerns found in Node.js.

  • Bun (41k ⭐) - A new JavaScript runtime built for speed, with a native bundler, transpiler, test runner, and npm-compatible package manager baked-in. Bun is written in Zig, a low-level programming language, and uses JavaScriptCore, the JS engine built for Safari, as its core. Bun aims to be a drop-in replacement for Node.js, with faster startup and running performance, lower memory usage, and better developer experience.

With introduction of mobile Javascript frameworks (React Native, NativeScript, Ionic, or Apache Cordova), you can now use JavaScript to develop truly universal apps that run in both iOS and Android. They allow you to develop UI elements using web technologies (HTML, CSS, React, Vue, Angular, etc.) and provide access to iOS and Android APIs via bridging.

  • React Native (RN) - The most popular solution to use JavaScript and React to develop mobile apps. In iOS, RN uses JavaScriptCore provided by platform. In Android, RN bundles the JavaScriptCore along with the application.

  • NativeScript - An open source framework for building truly native mobile apps with Angular, Vue.js, TypeScript, or JavaScript.

  • Ionic - An open source UI toolkit for building performant, high-quality mobile and desktop apps using web technologies — HTML, CSS, and JavaScript — with integrations for popular frameworks like Angular, React, and Vue.

  • Apache Cordova - An open-source mobile development framework. It allows you to use standard web technologies such as HTML5, CSS3, and JavaScript for cross-platform development, avoiding each mobile platforms’ native development language.

Typically, you create a desktop application for an operating system (OS) using each operating system’s specific native application frameworks. Electron makes it possible to write your application once using technologies that you already know: Chromium for displaying web content, Node.js for working with the local filesystem and the operating system, and Custom APIs for working with often-needed OS native functions.

  • Electron - A framework that enables you to create desktop applications with JavaScript, HTML, and CSS. These applications can then be packaged to run directly on macOS, Windows, or Linux, or distributed via the Mac App Store or the Microsoft Store.

Riding on the widespread popularity of JavaScript (particularly event-driven Node.js) in combination with low cost prototyping hardware, a new set of projects have emerged letting client side web developers use their skills to start controlling elements in the physical world.

  • Cylon.js - A JavaScript framework for robotics, physical computing, and the Internet of Things. It makes it incredibly easy to command robots and devices.

  • Tessel 2 - A robust IoT and robotics development platform. Leverage all the libraries of Node.JS to create useful devices in minutes.

Conclusion

In conclusion, JavaScript host environments play a crucial role in providing a runtime environment for executing JavaScript code outside of the browser. They offer a bridge between JavaScript and the underlying system or platform, allowing developers to leverage the capabilities and resources of different environments.

JavaScript host environments vary in their capabilities, APIs, and features. Each environment, such as Node.js, browser environments, or specialized platforms like Deno, has its own unique set of features, libraries, and tools tailored to specific use cases.

Understanding the characteristics and capabilities of different JavaScript host environments is essential for developers to choose the appropriate environment for their projects. It enables them to harness the power of the host environment’s APIs, interact with external resources, and build applications that target specific platforms or requirements.

Ultimately, JavaScript host environments contribute to the versatility and ubiquity of JavaScript, enabling developers to write code that can run in a wide range of contexts, from servers to browsers to IoT devices and beyond.