0

I have just noticed that when an undeclared variable has been called, JS is looking for the DOM element with such ID. Here's the example:

Let's say I have an HTML doc with the only content:

<div id="block"></div>
<script>console.log(block)</script>

And when I open the page and check the console, I will see the following:

screenshot

Though block hasn't been declared,it still has value. I never seen this before. I googled it but couldn't find anything. Is there a name for it and if possible, can someone drop caniuse page for that? Thanks everyone!

khandaniel
  • 306
  • 4
  • 13
  • 1
    Does this answer your question? [Do DOM tree elements with ids become global variables?](https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables) – N.J.Dawson Jun 19 '20 at 13:02
  • As well as the above - you might like to find out that you can write things like `Hello` and style them directly as if they were tags in CSS. Just another interesting quirk! (But don't do this - it's bad practice) – N.J.Dawson Jun 19 '20 at 13:03
  • 1
    Thanks @N.J.Dawson, this is exactly what I was looking for but for some reason couldn't find..! :) And thanks for this info about Tags, didn't know that Thanks, appreciate your answer! – khandaniel Jun 19 '20 at 13:10

0 Answers0