0

If I go to the developer console, say for this page, and type the following:

console.log(footer);

I get something like the following:

<footer id="footer" class="site-footer js-footer" role="contentinfo">

It appears that the id of the element doubles up as a variable. I haven’t yet declared or assigned the variable.

Where does this variable come from? I tried this in Firefox, Safari and Chromium, and they all do it.

I have also found that I can manipulate such a variable in real code without declaring it.

This actually makes debugging tricky, as I’m looking for an unset variable.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • 1
    The browsers all do it because it's actually mandated by the [HTML5 spec](https://html.spec.whatwg.org/multipage/nav-history-apis.html#named-access-on-the-window-object) even though it's a terrible relic from ancient times. It's probably best not to use these variables directly. Variables you declared yourself will always take precedence though, so you can use a linter in your source code to catch issues like accessing undeclared variables. – CherryDT Jul 27 '23 at 07:34
  • @CherryDT Thanks. That’s a better answer than simply referring to other decade-old discussions. – Manngo Jul 27 '23 at 07:39
  • 2
    @Manngo it's a decades old implementation that hasn't changed. The age of the duplicate is irrelevant. I wouldn't have linked it otherwise. Take note of the other [linked](https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-properties#h-linked) posts if you want more study material – Phil Jul 27 '23 at 07:48
  • 1
    @Manngo Well, I do think the link to the duplicate is valid though. It was the same thing a decade ago. We can always sort by newest answers to see if something relevant was added to the topic recently. We don't need multiple threads talking about the same thing. – CherryDT Jul 27 '23 at 07:48

0 Answers0