title: unable to call function to pass variable value from another javascript file
body: <p>I used a specific question name to avoid duplicate question names, since I found some posters had the same problem and I have used the solutions offered but still unable to call the function from another javascript file and use <code>alert()</code> to see the variable value from another js file passed to html page.</p>
  
  <p>In my file.js file, I have a function to declare an array and put a global variable:</p>
  
  <pre><code>function MsgsArr(){
  var Msgs = [
    "Msg1.",
    "Msg2.",
  ];
  return Msgs;
  }
  var Msgs_Arr = MsgsArr();
  </code></pre>
  
  <p>followed by a function to create a random message and declare global variable:</p>
  
  <pre><code>function CreateRandomMsg(){
  var RandomMsg = Msgs_Arr[Math.floor(Math.random()* Msgs_Arr.length)];
  return RandomMsg;
  }
  var Random_Msg = CreateRandomMsg();
  </code></pre>
  
  <p>Then it's the final function I want to achieve, to show the value based on the random message:</p>
  
  <pre><code>function ValuesToCheck(){
  
  if (Random_Msg == Msgs_Arr[0] || Random_Msg == Msgs_Arr[1]){
  var check = facesDet.photos[0].tags[0].attributes.glasses.value
  };
  return check;
  };
  </code></pre>
  
  <p>Since in the real case I have more than 2 messages, each 2 messages will share one value, the if statement will thus be longer. In my file1.html file, in a function I want to display the final value (true or false), and I use <code>alert(ValuesToCheck())</code> to call the function, but it doesn't work. If I declare the value like <code>var a = facesDet.photos[0].tags[0].attributes.glasses.value;</code> in here and <code>alert(a);</code> it will work. I'm not sure why things are not working when I'm trying to call the function from another js file, please help. Sorry the question seems lengthy to read. Thanks for taking your time to read it.</p>
  
  <pre><code>var facesDet = $.getJSON( APIdetect, function(facesDet) {
  })
  
  .done(function (facesDet, tid) {
      var tid = facesDet.photos[0].tags[0].tid;
      Load(tid);
      alert(ValuesToCheck());
  });
  </code></pre>