Hello, everyone.
So in this class on Java Script Intro the following code is described as good for use of block scope, yet it still polutes the namespace for reusing the variable name color twice.
const logSkyColor = () => {
const dusk = true;
let color = 'blue';
if (dusk) {
let color = 'pink';
console.log(color); // pink
}
console.log(color); // blue
};
console.log(color); // ReferenceError
How should it be rescripted in order to clear the namespace pollution?
Is this code i made better than the example in this aspect or nah? Did i change the code so that it might not work the way it was intended to?
const logSkyColor = () => {
const dusk = true;
if (dusk) {
let color = 'pink';
console.log(color); // pink
}else {
let color = 'blue';
console.log(color); // blue
}
};
logSkyColor();
I understand that this is a simple code and can be improved, but can every code be free of scope pollution? Or is inherent that the more complex the code, the higher will be the minimum of scope mistakes and namespace pollution? Because i feel that sometimes for clearing some pollution and improve scoping you might change the way your code works and it might not work the way you first intended to.
Thank you for your time!