How can the same 'const' be initialized multiple times in a for-loop? I thought 'const' can only be initialized once. I'm a beginning developer.
Does the const element get deleted each iteration and have to be redeclared? After a for-loop iteration loops each time, are all variables that were in its body deleted/lost?
const constDiv = document.createElement('div');
for (let i = 1; i <= 10; i++) {
const element = document.createElement('p');
element.textContent = 'This is paragraph number ' + i;
element.addEventListener('click', function respondClick(event) {
console.log('A <p> was clicked');
});
constDiv.appendChild(element);
}
document.body.appendChild(constDiv);