JavaScript JavaScript and the DOM Traversing the DOM Getting the First and Last Child

Why do up, down and remove have to be declared inside the function?


function attachListItemButtons(li) { let up = document.createElement('button'); up.className = 'up'; up.textContent = 'Up'; li.appendChild(up); let down = document.createElement('button'); down.className = 'down'; down.textContent = 'Down'; li.appendChild(down); let remove = document.createElement('button'); remove.className = 'remove'; remove.textContent = 'Remove'; li.appendChild(remove); }

if the declarations are made before the function in the form of const, then the function creates the buttons once and then moves them to the next item. (Only the last item ends up having buttons)