No you shouldn't. Variables declared using var
have function scope, not block scope!
Redeclaring a variable using var
might suggest that the variable is local to the loop/block when it isn't.
You could, however use let
to declare the variable, to ensure it is block-scoped.
for (let x = 1; x <= 3; x++) {
console.log(x)
}
for (let w = 65, x = String.fromCharCode(w); w <= 67; w++, x = String.fromCharCode(w)){
console.log(x)
}
console.log(typeof x) // undefined