while (input > 0) {
const quotient = Math.floor(input / 2);
input = quotient ;
}
How can I use const to declare a variable that would hold a different value each time a loop is iterated? From what I understand the reason we can use const to declare a variable in a for... of loop or in the block code of a regular for loop is because the loop creates a new lexical environment/scope each time the loop iterates, but that is not the case for while loops, it works with the same scope till the condition is met, so how is this possible?
I read that it redeclares the variable each time the loop iterates and that if I compare a variable that has the same value but happened in a different iteration it won’t be the same variable, for example:
let x = [];
let i = 0;
while (i < 2) {
const y = {};
x.push(y);
i++;
}
console.log(x[0] === x[1]); // returns false
meaning there are two variables with the same name in different memory spaces, how is this possible? Am I understanding correctly? I’m SO confused…