d3-book
d3-book copied to clipboard
Chapter 3 scope clarification (AKA who will gotcha the gotchas?)
The "Function-level scope" section of Chapter 3 (second edition, accessed on O'Reilly) mentions the following:
With block-level scope, our
i
would exist only within the context of the for loop, for example, so any attempts to read the value ofi
or changei
outside of the loop would fail. This is nice because you could establish other variables from within your loop and know that they wouldn’t conflict with variables that exist elsewhere.
Because we used var to declare the i
variable in the for loop, i
gets attached to the window (hey! that's the next section! 😄), so we actually could access it outside of the for block:
Compare this with declaring the variable with let:
This is horrible JS minutia that's absolutely outside the scope of this section's objectives, but it's caused me grief in the past, so I figured I'd point it out.