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
*smh* :)
Global Variables Considered Harmful
@aral So, i just scanned the code and couldn't see the problem... then realised I'd scanned it in such a way that I didn't see the call to greet() at the start I've hit similar (well, the same really) problems in PHP too...
I sometimes miss the way that C made us declare and initialise everything at the top of the code before we did anything else
@aral Me, as a C developer, not blinking: "Well of course!"
@aral Shouldn't even be able to call greet() in the first place! Undeclared!
*strokes long beard*
@jbaert Yeah, hoisting is fun (when it works) :)
@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 Ooh, I like that :)
@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
@aral I expected the initial greet() to fail because it was not defined ah, javascript