Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

WorkerGlobalScope.self — the worker global object

The read-only WorkerGlobalScope.self property returns the current worker's global object. Use self for explicit access to its interfaces and event handling; it is neither the window object nor access to the page's DOM.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

self

WorkerGlobalScope.self is the documentation name of an instance property, not a method call or a static property. In worker code, read it as self. It returns the current global object: DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, or ServiceWorkerGlobalScope, depending on the worker type. Consequently, self === self.self evaluates to true.

The property is read-only, but that does not freeze the returned object: for example, you can register event handlers through self.addEventListener(...). Inside a worker, self is not the Worker object controlled by the script that created the worker. A worker runs in its own context, without window or direct access to the page's DOM.

The presence of self alone does not prove that code is running in a worker: a page context also has Window.self, which returns a WindowProxy. WorkerGlobalScope.self is a Web API defined by the HTML standard, not a feature of a particular ECMAScript edition. MDN marks this property as widely available since July 2015; this does not imply identical support for every worker type and its APIs. Having Node 24 in a repository does not establish support for this browser API.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>WorkerGlobalScope.self</title>
</head>
<body>
  <script>
    const source = `
      self.postMessage([
        self === self.self,
        self instanceof WorkerGlobalScope,
        typeof window
      ]);
    `;
    const url = URL.createObjectURL(new Blob([source], { type: 'text/javascript' }));
    const worker = new Worker(url);

    worker.addEventListener('message', ({ data }) => {
      for (const result of data) console.log(String(result));
      worker.terminate();
      URL.revokeObjectURL(url);
    }, { once: true });
  </script>
</body>
</html>

Result

true
true
undefined

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources