Ran into this basic JavaScript scope gotcha today.
Just goes to show, you could be programming in a language for years and still get tripped up by some of the simplest issues.
https://codeberg.org/aral/gists/src/branch/main/javascriptScope.md
Ran into this basic JavaScript scope gotcha today.
Just goes to show, you could be programming in a language for years and still get tripped up by some of the simplest issues.
https://codeberg.org/aral/gists/src/branch/main/javascriptScope.md
@ekaitz_zarraga Yeah ;)
“It looks like it’s going to work because we’re used to things being hoisted in JavaScript but that only goes for functions.”
@aral If you want to learn more about it, it happens because `const` doesn't apply hoisting in the way you are expecting it to:
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
@ekaitz_zarraga Although, thanks for the link, I’ll add it to the gist :)
@ekaitz_zarraga @piggo Thanks, folks, updated the gist with your contributions:
https://codeberg.org/aral/gists/src/branch/main/javascriptScope.md
@aral it's not only for functions. It's way more complex than that.
It's a lot of fun.
@ekaitz_zarraga Yay! :)
@jbaert Yeah, hoisting is fun (when it works) :)
@aral Shouldn't even be able to call greet() in the first place! Undeclared!
*strokes long beard*
@aral Me, as a C developer, not blinking: "Well of course!"
@aral btw I know this because I've been teaching JS... if I didn't I wouldn't know about this.
These concepts are hard to find or learn about if you are just coding on JS.
@ekaitz_zarraga Yeah, teaching is the best way to learn ;)
@drhayes Ooh, I like that :)
@aral I'm sure you're sick of people popping in to explain this at you, so I won't do that.
But I *will* say this has the coolest name of anything I know of in programming: it's the TEMPORAL DEAD ZONE!
A touch of Star Trek in our JavaScript!
@drhayes Just updated the gist with a reference to your toot and the MDN explanation of Temporal Dead Zone :) Thanks again :)
https://codeberg.org/aral/gists/src/branch/main/javascriptScope.md
GNU social JP is a social network, courtesy of GNU social JP管理人. It runs on GNU social, version 2.0.2-dev, available under the GNU Affero General Public License.
All GNU social JP content and data are available under the Creative Commons Attribution 3.0 license.