There is a code:

<div id="div">текст</div> <script> div.insertAdjacentHTML('beforeBegin', '<p>Привет</p>'); div.insertAdjacentHTML('afterEnd', '<p>Пока</p>'); </script> 

I do not understand why the simple expression div.insertAdjacentHTML('afterEnd', '<p>Пока</p>'); works correctly.
Do not you first need to use getElementById () to find an element with id = div, assign this DOM object to a variable, and then insert something into it.
Why does this work?

    1 answer 1

    If the element is assigned a special attribute id , then you can get it directly in a variable with the name from the id value.

    This behavior complies with the standard . It exists, first of all, for compatibility, as a fragment of the distant past and is not very welcome, because it uses global variables. The browser tries to help us by mixing the JS and DOM namespaces, but conflicts are possible.

    But a more correct and common practice is to access the element by calling document.getElementById("идентификатор") .