Scope Chain: How JS Finds a Variable

Dina looks for a name starting where console.log is written, out from room to hall to lane until the first note. Then cetak() is called from the block: where…

Why it matters

Every time you write a variable, JavaScript looks it up through the scope chain. Knowing this explains why a variable is found or throws a ReferenceError, why an inner variable hides an outer one (shadowing), and it is the ground closures stand on. Most "why is the value not what I expected" bugs are about scope.

The key idea

The lookup stops at the first declaration, and it only goes one way

Common mistakes

Try it in this playground