Exploring JavaScript Runtime Environments: Web Browsers and Node.js
The JavaScript runtime environments let you run JavaScript code. As a JavaScript developer, understanding these environments will help you know where you can and cannot run your JavaScript code. In this article, I will explain the common JavaScript runtime environments, including their components and how they work.
What is a Runtime Environment?
A runtime environment is where your JavaScript code runs. There is more than one environment where your JavaScript code can run, but I'll only explain web browsers and Node.js runtime environments.
Web Browsers
A web browser is the primary place where your JavaScript code can run. As a frontend developer writing JavaScript code, the web browser is your first runtime environment. As a runtime environment, a browser has components that make your code work. They are:
JavaScript engine
Call Stack
Heap
Event Loop
Web APIs
Callback Queue
Microtask Queue
Concurrency Model
JavaScript engine
A JavaScript engine processes and executes your code. It has its own parts too. They are:
Parser
Interpreter
Just-In-Time Compiler
Garbage Collector
Parser converts your JavaScript code into an Abstract Syntax Tree (AST).
Interpreter executes it line by line.
Just-In-Time Compiler optimizes and compiles it into machine code for faster execution.
Garbage Collector controls how memory is given and recollected. It removes unused objects and frees up resources.
That's about the parts of a JavaScript engine. But I'm not done. I want you to know that different browsers use different engines. The Chrome browser uses a JavaScript engine named V8. Safari uses JavaScriptCore that is also known as Nitro. Firefox uses SpiderMonkey. And Microsoft Edge browser also uses Chrome’s V8.
Moving forward. Still on a web browser runtime environment parts.
Call Stack
Call stack is a data structure that tracks the active function calls and manages the execution context of JavaScript code.
Heap
Heap is a region of memory used for dynamic memory allocation where objects are stored.
Event Loop
Event Loop is a mechanism that manages the execution of asynchronous code and coordinates the execution of events and callback functions.
Web APIs
Browsers provide Web APIs. These APIs allow JavaScript to interact with browser features and external resources. They are:
Document Object Model (DOM)
Fetch API
Timers (setTimeout, setInterval)
Geolocation API
Canvas API
Storage API
WebSockets
DOM manipulates HTML and XML documents.
Fetch API handles network requests.
Timers schedules code to run after a delay.
Geolocation API retrieves the geographical location of the user.
Canvas API draws graphics and animations.
Web Storage API provides storage for key-value pairs in the browser.
WebSockets enables real-time communication with a server.
That’s all about web browsers runtime environment and the parts. Moving forward.
The Node.js
Node.js is a JavaScript runtime environment that allows JavaScript to work outside of browsers. And as a runtime environment, it also has its own parts that make your JavaScript code work. They are:
JavaScript Engine
Core Modules
Node Package Manager (NPM)
Event Loop
Libuv
Read-Eval-Print Loop (REPL)
JavaScript Engine
The Node.js runtime environment also uses the Chrome browser's V8 JavaScript engine. You already know what it does. It converts your JavaScript code into machine code computers understand, making it fast and efficient.
Core Modules
Core Modules are built-in modules that provide essential functionalities, like file system operations (fs), networking (net, http), path manipulations (path), and more. These modules are accessible without any installation.
NPM
NPM is the default package manager for Node.js. It allows developers to install, share, and manage third-party libraries and tools, which can be used to extend the functionality of Node.js applications.
Event Loop
Node.js uses an event-driven architecture, and the event loop is a core part of this. It handles asynchronous operations, like reading files or making network requests, allowing Node.js to be non-blocking and efficient.
Libuv
This is a multi-platform support library that provides Node.js with an asynchronous I/O and event-driven mechanism. It supports the event loop and handles tasks such as file system operations, DNS, network operations, and more.
APIs
Node.js provides a rich set of APIs for various functions, such as handling HTTP requests, interacting with the file system, and performing other I/O operations.
Read-Eval-Print Loop (REPL)
This is an interactive shell that allows you to enter JavaScript code and immediately see the results. It's useful for debugging and testing small snippets of code.
Conclusion
You've learned about the common JavaScript runtime environments including their parts and how they work. Understanding these runtime environments, their parts, and how they work is important as a JavaScript developer.
Remember, JavaScript runtime environment is where JavaScript code works. And the common runtime environments are web browsers and node.js. And as a runtime environments, they provide the tools that make JavaScript codes work.
Thank you for reading, and if you found this article helpful, please like, share, and leave your feedback.
Lets connect
Subscribe to my newsletter
Read articles from Shakir Bakare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shakir Bakare
Shakir Bakare
I'm a passionate frontend web developer with a love for creating beautiful and responsive web applications. With a strong foundation in HTML, CSS, JavaScript, and frameworks like React and Vue.js, I strive to bring innovative ideas to life. When I'm not coding, I enjoy sharing my knowledge through technical writing. I believe in the power of clear and concise documentation, and I write articles and tutorials to help others on their coding journey. Let's connect and create something amazing together!