mastodon.ar.al is one of the many independent Mastodon servers you can use to participate in the fediverse.
This is my personal fediverse server.

Administered by:

Server stats:

1
active users

Aral Balkan

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.

codeberg.org/aral/gists/src/br

Codeberg.orggistsA place for me to post and share small, self-contained code snippets.
@aral of course, if you used 'var' instead of 'const', it would work. No, it doesn't make sense
@aral var and function work the same way. const and let, added much later, work more "correctly". but as a result the language is even more inconsistent 🙃

@aral

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!

@aral I expected the initial greet() to fail because it was not defined 🙃 ah, javascript