Episode 19 game · Raiden · Lightning
Raiden’s Dead Zone
Kai changed var to const and his booking page went blank: Cannot access 'API_URL' before initialization. Feel the ghost that lives between the top of a scope and a declaration, put the code in the order it runs, then prove you can tell which snippets throw before you run them.
app.js, declared with const
- Nothing has run yet.
Line 1 runs first. Function declarations are ready before any line runs.
1 · Wake the ghost
Same code, two keywords. Run Kai’s app with var, then with const, and compare what the console says.
2 · Put it in the order it runs
Pick the change that really fixes each file. Hiding the error does not count.
3 · When does it run?
For each snippet, predict: does it give a value, or throw a ReferenceError? Then run them for real in this browser.
New seal earned
The Yurei is at rest
Acharya’s Rule #19Declare before you use. Hoisting makes the name, not the value.
A scope creates all its names first. Functions get their body, var gets undefined, let and const get no value.
From the top of the scope until the declaration runs, reading a let or const throws, even with typeof.
Constants first, the call that starts everything last. Turn on ESLint’s no-use-before-define, and never go back to var to hide it.