4. Understanding the JS Runtime
When you run JavaScript, it's executed within a specific environment called the Runtime Environment. In the browser, this environment includes several components.
The JavaScript Engine
The core component is the JS Engine (e.g., V8 in Chrome). It takes your code and executes it. The Engine contains:
- Memory Heap: Where variables and objects are stored.
- Call Stack: Where the execution context (which function is currently running) is tracked.
The Browser's Role
Beyond the Engine, the browser provides Web APIs (Application Programming Interfaces).
- Examples of Web APIs:
setTimeout, DOM (Document Object Model),fetch(for networking).
When JS executes, if it encounters a browser-specific command (like 'wait 2 seconds' or 'change this element'), it hands that task off to the Web APIs and continues running the rest of the code. This non-blocking behavior is crucial for understanding advanced JS.