title: Assign value returned from a javascript function to a variable
body: <p>I have a function that returns a string, and I need to call the function several times and put the result in a variable.</p>
  
  <p>I have this:</p>
  
  <pre><code>function GetValue() {
      ... Do Something ...
      console.log(string); // displays 'string'
      return string;
  }
  </code></pre>
  
  <p>And I'm trying to assign the output of the function to a variable in another function. Currently I'm doing this:</p>
  
  <pre><code>var theString = GetValue();
  console.log(theString); // displays 'undefined'
  </code></pre>
  
  <p>What I don't understand is the console output in the function is displaying the value, but it's not after assigning the variable to the output of the function.</p>
  
  <p>Obviously this isn't the way to assign the output of a function to a variable. So how do I do that?</p>
  
  <p><strong>[ADDITIONAL INFO]</strong></p>
  
  <p>Apparently my attempt at being brief in my sample code only served to cause confusion.</p>
  
  <p>Here is the full function that I need to call several times from elsewhere in the javascript file:</p>
  
  <pre><code>/*
  * Build URL link
  */
  function BuildUrlTargetFolder() {
      // Get the current url and remove the 'zipCodes/branchAdmin' part of it for the branch url link
      var urlArray = window.location.href.split('/'),
      targetLength = urlArray.length - 3,
      i,
      targetFolder = '';
  
      for (i = 0; i &lt; targetLength; i++) {
          // Only add a slash after the 'http:' element of the array
          if (i === 0) {
              targetFolder += urlArray[i];
          } else {
              targetFolder += '/' + urlArray[i];
          }
      }
  
      console.log('targetFolder: ' + targetFolder); // console output is the current url minus two "levels"
  
      return targetFolder;
  }
  </code></pre>
  
  <p>Here's one of the places that the function needs to be used:</p>
  
  <pre><code>var targetFolder = BuildUrlTargetFolder();
  
  console.log('targetFolder: ' . targetFolder); // console output: "undefined"
  
  // If the url has a value, set the href attribute for the branch link otherwise hide the url
  if (data['url'] !== '') {
      $('a#branchLink').attr('href', targetFolder + '/index.php/coverage-area/' + data['url']).show();
  } else {
      $('a#branchLink').attr('href', '#').hide();
  }
  </code></pre>
  
  <p>So, having said that, how do I get the string from the function assigned to the calling code's variable?</p>