# Why does it work?

**URL:** <https://forum.kirupa.com/t/why-does-it-work/652382>\
**Category:** web dev\
**Created:** [June 24, 2022, 5:22pm UTC](https://forum.kirupa.com/t/why-does-it-work/652382 "2022-06-24T17:22:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![slyuqi](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/slyuqi/32/18085_2.png) [@slyuqi](https://forum.kirupa.com/u/slyuqi)\
**Post date:** [June 24, 2022, 5:22pm UTC](https://forum.kirupa.com/t/why-does-it-work/652382/1 "2022-06-24T17:22:08Z")

</div>

```auto
let myButton = document.querySelector('#myButton');
myButton.addEventListener("click", doSomething, false);
function doSomething(e) {
    alert('is this working?');
}

```

Why the code works even if the “doSomething” function was defined after using it???

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 24, 2022, 8:07pm UTC](https://forum.kirupa.com/t/why-does-it-work/652382/2 "2022-06-24T20:07:13Z")

</div>

Due to [hoisting](https://www.kirupa.com/html5/hoisting.htm)! 🎉
