AleksSergSB,
for(
let i=0; i<itemArray.length; i++)
<!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.item {
width: 50px;
height: 50px;
background-color: #2320CC;
margin: 5px;
}
</style>
</head>
<body>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<script>
const itemArray = document.getElementsByClassName('item');
for(let i=0; i<itemArray.length; i++) {
itemArray[i].onclick = function (){
alert(i);
}
}
</script>
</body>
</html>