for(let i = 0; i < 5; i++) {
console.log(i); //outputs 0, 1, 2, 3, 4
}
HERE ↓ IS THE DOUBT
for(let i = 0; i < 5; i++) {
let i = 2;
console.log(i); //output 2 five times
}
Why am I able to initialize and assign the i
variable twice, as I know we can only initialize and assign the variable declared with let
once and later can reassign it another value. For example:
let j = 5;
let j = 6;
console.log(j); //error > Identifier 'i' has already been declared
let k = 5;
k = 6;
console.log(k); // output 6