Live data from Hacker News

Named element IDs can be referenced as JavaScript globals

css-tricks.com

1–10 of 113 posts

Re: Named element IDs can be referenced as JavaScript globals

#8
Another similar gotcha is that the global-scoped `name` variable must be a string. See https://developer.mozilla.org/en-US/docs/Web/API/Window/name for details.

    var name = true;
    typeof name; // "string", not "boolean"
Luckily, this is not true within ES modules which you probably use most of the time anymway.

Re: Named element IDs can be referenced as JavaScript globals

#9
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

Avoid globals at all costs - use IIFE [1] instead, wrapping your function in parenthesis and invoking it right away.

[1] https://developer.mozilla.org/en-US/docs/Glossary/IIFE

Re: Named element IDs can be referenced as JavaScript globals

#10
post #7

This is one of those things that pops up every year or two years. Unfortunately, the person writing about the new discovered weird trick almost always fails to precede the article with a big, red, bold "Please don't ever do this".

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this:

https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

Post reply on HN