Skip to the solution
EasyFrontend5 min read

What is globalThis?

532 views

The problem

Question presented to candidate: "Before globalThis existed, how would you reliably access the global object in code that needed to run in BOTH a browser and Node.js, and what problem does globalThis actually solve?"

What a strong answer should cover:

  • πŸ“Œ Interview term: globalThis β€” a standard (ES2020), environment-independent reference to the global object, working identically whether the code runs in a browser (where it was previously window/self), Node.js (previously global), or a Web Worker (previously self).
  • πŸ“Œ Interview term: the real, direct answer to the prompt's problem β€” before globalThis, code that needed to run in multiple environments had to use a real, awkward feature-detection dance (checking typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : self) just to reliably reach the global object β€” globalThis genuinely eliminates that entirely with one universal name.
  • πŸ“Œ Interview term: it's the same underlying object, just a universal name β€” verified directly: setting a property via globalThis.x = value genuinely makes that property visible as a plain global identifier in the SAME environment β€” globalThis doesn't create a new, separate global scope; it's a real, direct reference to the one that already exists.
  • A precise answer names that globalThis is genuinely useful for polyfill/feature-detection code and libraries that must run correctly in multiple JavaScript environments without knowing in advance which one they're in β€” the real motivating use case the standard was created for.
  • A precise answer names that despite globalThis existing, directly relying on implicit globals (assigning to an undeclared variable, reading an unexpected global) remains poor practice in application code β€” globalThis solves environment PORTABILITY, not the general advice to avoid polluting global state.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; naming the real cross-environment problem it solves (not just "it's the global object") is the strong signal.

Code / implementation expected: Optional β€” showing a property set via globalThis becoming visible as a plain identifier demonstrates it is the real, same global object, not a separate abstraction.

  • globalthis

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: globalThis is a direct reference to the same global object, not a separate abstraction

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 21 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is the difference between Object.is, ==, and ===?

Continue