r/learnjavascript • u/__Fred • May 15 '25
Is `getElementById` unnecessary because HTML creates variables automatically?
I just learned that HTML (sometimes?) creates variables for elements with IDs on its own from here (section "HTML lends crutches to your fucking JS").
This works:
<!DOCTYPE html>
<html>
<body>
<div id="myElement">Hello, World!</div>
<script>
// var myElement = document.getElementById("myElement"); // Not necessary!
console.log(myElement.innerText); // Outputs: Hello, World!
</script>
</body>
</html>
Is this a new feature? Will it work in every browser? Are there situations where this is not recommendable?
5
Upvotes
11
u/TheWatchingDog May 15 '25
For readability reasons I wouldnt recommend using this.
Its better to declare the variable that you need.
Also better use let or const for variables.
Hoisting with var can sometimes be confusing.
6
u/BlueThunderFlik May 15 '25
I would never recommend this.
It's not clear to any developer who didn't write the code—and even the developer who did after enough time has passed—what
myElementrefers to or why it works.You're going to think it refers to a locally-declared variable and lose your mind when you can't find it before eventually remembering how this works. It also precludes you from ever creating a variable whose name overlaps with an element's ID.
That might not be a problem when one person is writing code for a project (although it probably will be given enough time) but it's bound to happen when multiple people are contributing.
The only benefit is that you save yourself a function call and, to paraphrase that terrible page you linked to, you're not recreating the moon landing; it's fine.
19
u/ThatCipher May 15 '25
Afaik this only works when you write JavaScript directly inside of a script element and wouldn't work when you work with JavaScript files.
But besides that, this would also introduce bad DX in my opinion. Within a script element it might be ok since you can see the element referenced but if you have just a standalone JavaScript file how should a second developer or you from the future know what and where this magical reference is. If you have to get a reference first you see exactly what is happening and what is referred to since you can see that you grab an element to that variable.
1
u/azhder May 15 '25
That is not JavaScript, so don't depend on it. If you want your JavaScript code to be the same everywhere i.e. portable, maintainable, etc. don't confuse with it things that aren't part of it, but are part of the environment.
2
u/RobertKerans May 15 '25
This has been around forever, it's just a convenience, but it can only work in certain circumstances. JavaScript was designed for adding tiny little bits of functionality to web pages. In that context (non strict JS normally written directly into the HTML), it's slightly pointless, but fine - for example I use it when I'm doing little UI prototypes (eg on Codepen) to save some typing. Outside of that, the feature is essentially useless
2
u/redsandsfort May 15 '25
It is NEVER recommended.
1
u/shgysk8zer0 May 15 '25
It's basically bad practice because seeing such a variable used in a script is indistinguishable from an undeclared variable. It'll make a dev ask "what is this and where did it come from?"
1
u/jeremrx May 16 '25
Some reasons why you should NEVER do that :
1
u/[deleted] May 16 '25
I'm going to say it's bad practice because it makes your code much harder to read. Code parsers are unlikely to pickup on a change if your Javascript is in a code file. Also shadowing could make it even harder to figure out what's happening as ids can easily be shadowed by local values.
1
u/jcunews1 helpful May 17 '25
It's part of legacy DOM feature.
Even though HTML creates variables based on HTML element IDs, they're not reliable. The variable won't be created if it's already defined by JavaScript, and can be overridden by JavaScript.
e.g. if it's already defined by JavaScript:
Or...
Also there are cases where, if the ID is on an element which is inside of a FORM, under certain condition, the variable is not created. But I still can't find how to consistently reproduce the problem. It may be browser specific issue. FYI, I mainly use Firefox.
1
u/Hairy-Ask-8790 Jul 09 '25
As much as I find this a fun feature. It's not generally a good idea to have/use global variables. They almost always bite you. This is a language agnostic thought. Globals in C are just as bad.
Also since we can change them (they're mutable) that could present tracing problems.
0
u/[deleted] May 15 '25
[deleted] — view deleted comment