Why are const declarations within a while loop not an issue? Isn't the same variable being redeclared in each iteration?
In the code below, the arrValue variable is declared within the loop. I felt this would be an issue but it runs fine without errors.
Usually, I would have declared the let arrValue; right before the loop and accessed it within the loop without having it redeclared.
const arr = [1, 2, 3, 4, 5];
let sum = null;
while (arr.length > 0) {
const arrValue = arr.pop();
sum += arrValue;
}
console.log(sum);
const(andlet) are block scoped variablesI would have declared the const arrValue; right before the loop- which would cause your code to fail since a) you need to assign a value when declaring a const and b) you would then try to assign a new value to a const - which would fail, asconstis a constant value