Recently I was surprised that both of these code blocks work just fine (see snippet):
for (let propName in myObject) {
console.log(propName);
}
for (const propName in myObject) { // A const THAT WILL CHANGE VALUE ?
console.log(propName);
}
It seems like I'm using a const
that it's changing value.
QUESTION
How can this be possible and is one better than the other? Regarding memory allocation and performance?
const myObject = {
propA: 'foo',
propB: 'bar'
};
console.log('For in with let:');
for (let propName in myObject) {
console.log(propName);
}
console.log('For in with const:');
for (const propName in myObject) {
console.log(propName);
}