3

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>
Su Ming Yuan
  • 125
  • 12
  • 3
    Your `global` variable is not assigned with any value globally. It gets its value from `functionA`. Make sure you call `functionA` first and then other functions that uses value of `global` variable. – Karan Desai Sep 13 '17 at 03:03
  • I can't completely parse your English. You say the global variable becomes undefined? When you say "global variable when set to local variable", do you mean "global variable, when that global variable is assigned the value of a local variable", or "global variable, when a local variable variable is assigned its value"? Note that unlike many other languages, "set A to B" in English means "change the value of A so that it now has the value of B", not "take the value of A and place (set) it INTO B". –  Sep 13 '17 at 03:08
  • 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. – Su Ming Yuan Sep 13 '17 at 03:14

2 Answers2

0

Can you try this

var global = "text";
function  functA{
     var lol = global; 
     dostringprocess(local);
  }

I think in your code it taking local variable from FunctioA, and also you can check in two ways

  1. open console and see the log what error it showing exactly by putting some log message in every function in function stack

  2. you can use closure

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Balaraju M
  • 473
  • 1
  • 3
  • 14
0

since your global value is defined under a functionA() so when you call a function B() or functionC() directly your global variable does not contain any value.. so for this you have to intialise the value of global variable..

var global="Earth";                      // now it will not give any errror

 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>
  • although it is not going to get "undefined", it is not certain to get "global value" in these functions. It may get "Earth" instead of "undefined". – Su Ming Yuan Sep 13 '17 at 03:48