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
Thinking an inner variable "overwrites" the outer one It is not overwritten, only found first. The outer one is still there, just shadowed. Remove the inner declaration and the outer one is read again.
Thinking scope is set by whoever calls the function Scope is lexical: set by where a function is written, not where it is called from. Unlike this, which is set at the call.
Thinking the lookup can go up AND down One way only: inside to outside. An outer scope cannot read a variable declared in an inner one; that is why a let in a block does not leak out.
Try it in this playground
Remove the block’s nama From the block, Dina goes out to sapa(): found there.
Turn nama on in all three scopes The nearest one is found first; the outer ones are still there (shadowing).
Turn every nama off Dina reaches the end, global, and finds nothing: ReferenceError.
cetak() outside sapa() Called from the block, yet it looks from where cetak is written: lexical.