For some reason a JavaScript global variable becomes undefined inside function when set to local variable sometime.
For example, in below sample the local
often become "undefined
".
Could anyone of you please advise?
I'm expecting the value of "local
" is "global value" in functionB()
and functionC()
or doStringProcessingA()
and doStringProcessingB()
. The value of "local
" is "undefined
". I'm sure there's no other places assign value or set undefined to global in anywhere.
I've checked below links, but it seems not related. 'Hoisted' JavaScript Variables and Why a variable defined global is undefined?
functionA() was called by onClick event from HTML.
var global;
function functionA(){
global = "global value";
}
function functionB(){
var local = global;
doStringProcessingA(local);
}
function functionC(){
var local = global;
doStringProcessingB(local);
}
<div onclick="functionA()">
<span class="Text">Submit</span>
</div>