Skip to content

Limitations

Web Workers run in a separate thread with restricted access to browser APIs.

// ❌ Cannot access:
// - DOM (document, window)
// - Parent object
// - Most browser APIs (localStorage, etc.)
// - The main thread's variables or functions
// ✅ Can access:
// - navigator (limited info)
// - setTimeout, setInterval
// - XMLHttpRequest, fetch
// - WebSockets
// - IndexedDB
// - importScripts() for loading external libraries
// importScripts() loads external scripts synchronously
importScripts('lodash.js');
importScripts('math.js', 'validator.js'); // Multiple files
// Now you can use the loaded libraries
const result = _.chunk(array, 2);
  • No DOM access (document, window, parent)
  • Can use fetch, WebSockets, IndexedDB
  • Load libraries with importScripts()
  • Workers are isolated from the main thread’s scope