Below is a trivialized version of a solution I came across while trying to implement a version of the Jquery CSS function.
function CSS(prop, value) {
...
if (value === undefined) {
// No matching elements.
if (element == null) {
return undefined;
}
const value = element.style[prop]; // <-- Why is this allowed?
return value === "" ? undefined : value;
}
...
}
I noticed that the function param value
was being redeclared and automatically raised an eyebrow to the solution. I threw it in a code sandbox and it worked. I checked the docs and found exactly what I expected:
“Redeclaring the same variable within the same function or block scope using let is not allowed in JavaScript.”
Now this specified let
but even chaging to that still works.
If I bring the declaration out of the if statement, I get the duplicate error I’d expect so this clearly has to do with the block scope of the if statement. However, because it inherits the function scope outside of that which includes a variable of the same name a redeclartion should be illegal still, no?
What am I missing?