Scoping in Javascript is somewhat strange in that variables declared in an new scope will not shadow variables previously declared in a previous scope. Your question demonstrates the textbook example of what that means.
The most compatible way to do this is to just rename your variable in your for loop:
var index = 8888;
console.log(index);
for (i in a) { // by the way a.length = 5
console.log(i);
}
The next most compatible way is to use the forEach function:
var array = ['a', 'b', 'c'];
array.forEach(function (current, arrayIndex) {
console.log(arrayIndex);
});
The ECMA6 way that is officially approved but may still not be fully implemented by browsers is to use the let keyword.
var index = 8888;
console.log(index);
for ( let index in a) { // by the way a.length = 5
console.log(a[index]);
}
console.log(index); // still 8888
From the documentation:
let vs var
When used inside a block, let limits the variable's scope to that block. Note the difference between var whose scope is inside the function where it is declared
It continues:
You can use the let keyword to bind variables locally in the scope of loops instead of using a global variable (defined using var) for that.
let is pretty much Javascript trying to make there scoping less crazy in a backwards compatible way.