0Pricing
JavaScript Academy · Lesson

Globals, fetch availability, file/network APIs

Detect runtime safely (globalThis/window/global), check fetch availability, and understand why files need Node while browser focuses on DOM/network.

Big picture

Goal: Know where your code runs.

  • Globals: globalThis, window, global
  • fetch: feature-detect, not assume
  • Files: Node has fs; browsers do not
  • URLs: URL works in both
Globals, fetch availability, file/network APIs — illustration 1

Runtime sniff (safe)

Use typeof checks to avoid ReferenceErrors and learn where you are running.

// Detect presence of common globals
const hasWindow = typeof window !== "undefined";
const hasGlobal = typeof global !== "undefined";
const hasProcess = typeof process !== "undefined" && process && typeof process.version === "string";

console.log("globalThis exists?", typeof globalThis !== "undefined");
console.log("window?", hasWindow);
console.log("global?", hasGlobal);
console.log("Node process?", hasProcess);
Globals, fetch availability, file/network APIs — illustration 2

All lessons in this course

  1. Globals, fetch availability, file/network APIs
  2. ESM loader quirks, path/URL differences
  3. Env variables & config patterns
← Back to JavaScript Academy