Course contentsShow
← Web Development
Lesson 2090 of 2,98238. JavaScript Performance in the BrowserFree lesson

Worker Scope and Available APIs

Understanding the worker global scope, what APIs are available, and what's restricted (no DOM access).

Worker Scope and Available APIs

You'll learn what's actually available inside a Web Worker and what critical limitations exist.

When you create a Web Worker, it runs in a completely separate execution context from your main page. Think of it like sending an employee to work in a remote office — they have their own workspace with different tools and resources than the main headquarters.

The Worker Global Scope

Inside a worker, the global object is not window — it's WorkerGlobalScope (or more specifically, DedicatedWorkerGlobalScope for standard workers). This scope provides self as a reference to the worker itself, which you use for event handling:

// Inside worker.js
self.onmessage = function(e) {
  // Worker code here
};

What's Available

Workers have access to:

  • Core JavaScript: All standard built-ins (Array, Object, Promise, etc.)
  • Timers: setTimeout, setInterval, clearTimeout, clearInterval
  • Network: fetch API, XMLHttpRequest
  • Storage: IndexedDB, Cache API
  • Console: console.log() for debugging
  • Messaging: postMessage, onmessage (as you've learned)

Critical Restrictions

Here's the dealbreaker: Workers cannot access the DOM. No document, no window, no getElementById. They also can't directly manipulate the page's UI or access most browser APIs tied to the visual layer.

Why? Workers run on separate threads. The DOM isn't thread-safe, so allowing direct access would cause race conditions and crashes. Workers compute; the main thread renders.

Key Takeaway: Web Workers operate in an isolated scope with access to core JavaScript and data APIs, but are completely cut off from DOM manipulation — they're pure computation environments that communicate results back to the main thread.