What is a Runtime?
What you'll learn: The runtime is the environment that surrounds the JavaScript engine and gives your code the ability to interact with the outside world.
Beyond the Engine
You've learned that the JavaScript engine (like V8, SpiderMonkey, or JavaScriptCore) is responsible for parsing and executing your code. But here's the crucial part: the engine alone can't do much beyond math and logic.
The engine doesn't know how to:
- Read or write files
- Make network requests
- Display graphics on a screen
- Handle timers or events
That's where the runtime comes in.
What the Runtime Provides
Think of the engine as a powerful calculator. It's brilliant at computing, but it has no hands, no eyes, no ears. The runtime is like the desk, tools, and assistants surrounding that calculator—it provides the APIs and services that let your code actually interact with the world.
Browser Runtime
When JavaScript runs in a browser, the runtime includes:
- The DOM (Document Object Model) for manipulating web pages
- APIs for network requests
- Timers and event handlers
- Access to browser features like location and storage
Node.js Runtime
When JavaScript runs in Node.js (on a server), the runtime includes:
- File system access for reading/writing files
- Network capabilities for building servers
- Operating system interactions
- No DOM (because there's no web page)
The Complete Picture
Engine + Runtime = Complete JavaScript Environment
The engine executes your code. The runtime provides the surrounding tools and APIs that make your code useful in a specific context.
Key Takeaway: A runtime is the environment wrapping the JavaScript engine, providing APIs and services (like file I/O, network access, or DOM manipulation) that let your code interact with the outside world—different runtimes (browser vs. Node.js) provide different capabilities.