title: Javascript variable scope inside ajax function
body: <p>i know this is probably a stupid question and i know js scope questions are extremely common on SO, but could you possibly be kind enough to help me with this one anyway.</p>
  
  <p>Here is my function:</p>
  
  <pre><code>function checkOpen(storeData) {
  if (storeData.menu === 'true') {
  
      var offerImgPath = '/wcsstore/ConsumerDirectStorefrontAssetStore/images/colors/color'+ Brand.styleColour + '/v3/',
          offer1,
          offer2;
  
      cafeTabContent = '&lt;p&gt;Cafe/Restaurant facilities are available in this store.&lt;/p&gt;';
      cafeTabContent += '&lt;p class="menu_link"&gt;&lt;a href="/store_menus/menu.pdf" target="_blank"&gt;Click here to download a menu&lt;/a&gt;&lt;/p&gt;';
  
      if (Brand.storeLocatorSettings.cafeOffersOn === true) {
  
          $.ajax({
              url : Arcadia.Brand.storeLocatorSettings.offersPageURL,
              success : function(data) {
                  var offerContainer = $(data).find('#basic_story_content');
                  offer1 = offerContainer.children(':first-child').attr('src');
                  offer2 = offerContainer.children(':last-child').attr('src');
              }
          });
  
          console.log(offer1);
  
          cafeTabContent += '&lt;p&gt;Current offers:&lt;/p&gt;';
          cafeTabContent += '&lt;p class="offer_graphic"&gt;&lt;img src="' + offer1+ '" alt="Offer 1" /&gt;&lt;/p&gt;';
          cafeTabContent += '&lt;p class="offer_graphic"&gt;&lt;img src="' + offer2+ '" alt="Offer 2" /&gt;&lt;/p&gt;';
      }
  
      return cafeTabContent;
  } else {
      return false;
  }
  };
  </code></pre>
  
  <p>The problem that i am having is that despite the fact that offer1 &amp; offer2 are defined outside the ajax success function, the values applied within this function are not being maintained outside hence console.log(offer1) returns undefined instead of the image path. I know this is a scope question, and it is really bugging me. Any help would be greatly appreciated.</p>