I ran into this code earlier and I don't understand why the list item's number on the array index is logged to the console when you click on a list item. Shouldn't the console log 4 every time a list item is clicked? Better yet, shouldn't it be impossible to click on any of the list items because items[x] is equal to 4? What am I not understanding here?
html
<ul>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
javascript
var items = document.getElementsByTagName("li");
for(let x=0; x < items.length; x++){
items[x].onclick = function(){
console.log(x);
}
}