Search lands in PR-5.1 (Pagefind).

Tutorial Beginner

Chapter 3 Updated

First Code — REPL, Files & Globals

Install Node, the REPL, your first file, and the `global` / `globalThis` / top-level `this` puzzle.

  • Full 15m
  • Revision 4m
  • Flow 2m

Install + verify

From nodejs.org to a live terminal.

  • Download the LTS build from nodejs.org.
  • Installer bundles Node.js + NPM — both arrive together.
  • Verify with node -v and npm -v.
  • “Command not found” → PATH issue; restart terminal or reinstall with Add to PATH checked.

REPL — the playground

Read, Evaluate, Print, Loop.

  • Type node in the terminal; exit with .exit or Ctrl+C twice.
  • Reads one line, evaluates via V8, prints the return value, loops back.
  • let x = 5 prints undefined — declarations have no return value.
  • Good for experiments; bad for real projects (nothing persists).

File mode

The real workshop.

  • Create a folder → open in VS Code → create app.js.
  • Run with node app.js — the whole file executes top to bottom.
  • No HTML, no <script> tag, no browser needed.
  • Use the integrated terminal with Ctrl+` to keep editor + runner in one place.

global vs window

Every runtime has a global object.

  • Browser: window — DOM, alert, localStorage.
  • Node.js: globalsetTimeout, setInterval, setImmediate, console, fetch.
  • global is provided by Node, not V8 — V8 itself knows nothing about it.
  • Node has no DOM, no alert, no prompt.

top-level this + globalThis

The interview trap.

  • In a Node file, top-level this is module.exports — an empty {}, not global.
  • In a browser, top-level this is window.
  • globalThis (ES2020) is the universal reference — window in browsers, global in Node.
  • Use globalThis when you want isomorphic code; use global inside Node when you don’t care about portability.

Comments

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