Search lands in PR-5.1 (Pagefind).

Explanation Beginner

Chapter 5 Updated

window, this, undefined vs not defined

The global object, the this keyword, and two commonly confused concepts.

  • Full 14m
  • Revision 3m
  • Flow 2m

The shortest JS program

An empty file still does a lot.

  • Even an empty file creates the Global Execution Context.
  • The window object is created — the global object in browsers.
  • this is created and points to window at the global level.
  • Memory space + execution thread are set up, ready for your code.

window & the global scope

Where var lives, where let/const don't.

  • var x = 10 at global scope → attached to window. window.x === 10.
  • let / const at global scope → Script scope, NOT on window.
  • Web APIs (console.log, setTimeout, alert, fetch) are properties of window — they’re not part of JS itself.
  • globalThis (ES2020) = universal global object: window in browser, global in Node.

this — the eight contexts

How a function is called decides this.

  • Globalthis === window (browser) or global (Node).
  • Regular function standalonewindow (non-strict) or undefined (strict).
  • Object method → the owning object.
  • Arrow functionno own this — inherited from the enclosing lexical scope.
  • Constructor (new) → the new object. Event handler → the DOM element. call/apply/bind → whatever you pass.

undefined vs not defined

A value vs an error.

  • undefined — declared, no value yet. Memory allocated. It’s a type. typeof undefined === "undefined".
  • not defined — never declared. No memory. Access → ReferenceError: x is not defined.
  • Three states: declared + assigned (value), declared only (undefined), never declared (error).
  • typeof is safe on undeclared variables — still returns "undefined", no error.

undefined vs null

Engine default vs programmer intent.

  • undefined = JS hasn’t assigned a value. null = programmer chose no value.
  • typeof null"object" — a historical bug, preserved for back-compat.
  • undefined == nulltrue (loose). undefined === nullfalse (types differ).
  • Never assign undefined manually — use null when you mean “empty.”

Falsy & truthy

The 8 falsy values — and the traps.

  • Falsy: false, 0, -0, 0n, "", null, undefined, NaN.
  • Truthy traps: "0", "false", [] (empty array), {} (empty object) are all truthy.
  • Missing object property → undefined (no error). Missing function arg → undefined.
  • void 0 → a reliable way to get undefined if something has shadowed it.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.