This was an embarrassing question about something I thought I fully understood about global variable scope. I'm re-editing it, despite knowing the answer now, because it might save someone else a LOT of grief. Originally I had a variable declared on a page, that wasn't being "seen" in a linked .JS file, thus over complicating the situation. Now, however, I see how the problem can be reproduced very simply. Here is some example code illustrating the problem.
script>
var myGlobalVar = 100;
function testMyVar() {
if (typeof myGlobalVar == "undefined") var myGlobalVar = 200;
alert ("var is now: " + myGlobalVar)
}
</script>
Now lets say you call the function, maybe from an onclick event of a button like this...
<button onclick="testMyVar()">TestVar</button>
Imagine my surprise when the "alert()" function displayed "200"! Why? Again the original case where I first observed this behavior was when the function was in in a linked "JS" file. The idea was to allow me to detect whether the variable had been declared in the page. If it had not been declared, I would declare it and give it a 'default' value. But now, having reproduced the issue in the simple demo above, I see it does not matter whether the function is in a JS file or not.
So why did it appear as if "myGlobalVar" was being detected as undefined? The answer, which i now know, came when I modified my function code like this...
script>
var myGlobalVar = 100;
function testMyVar() {
var myLocalVariable = 200;
if (typeof myGlobalVar !== "undefined") myLocalVariable =myGlobalVar;
alert ("var is now: " + myLocalVariable)
}
</script>
Now, when the function is called, the expected value "100" is displayed. And if the line declaring "myGlobalVar" is removed, the same function now properly detects that it doesn't exist, and uses my default value "200"
Having seen this, it seems to me this is probably the best example of how variable "hoisting" can come back to make you think you're going insane! In the original function it may appear that I am declaring and assigning a value to "myGlobalVar" only after I have tested and proven that it doesn't exist. But that is NOT what is happening! This must be the result of variable "hoisting" behavior of javascript, which caused the variable declaration and definition (200) to actually occur before the test, rather than the sequential manner in which the code makes it appear.
So now I know I can NOT declare a variable within a function using the same name as a global, if I have any intention of testing for the global version. Because no matter where in the function I declare a variable, its actually going to me moved (hoisted) to the top of the function. So if it has the same name as the global, its going to replace the global before I can test it!
To turn this into a question, as that's what this forum is for, I'll just ask: Am I correct now in my understanding of seemingly strange behavior of the first example?
"undefined"?dimBackgroundfunction? What is the value ofdimColorbefore theifstatement? What is it afterward? Note also that your guard here just creates a new local variable within that function whose value is'#003333', sodimColorelsewhere (outside the function) could well still beundefined.