title: A variable is not getting initialized in ajax call response- Javascript
body: <p>I am making an Ajax call in a another function <code>function checkUrl(url){...}</code>. When this function is called and Ajax request has been sent, and a response is got. I have declared a variable <code>var str;</code> inside this <code>function checkUrl(url){...}</code>.  function, which is not in the scope of the Ajax function, so it means that variable <code>str</code> is global to the Ajax function. What I need is, when I get the response I want to assign that response to that global variable. But due to some reasons the variable is not getting to be initialized and its <code>return</code> statement is returning undefined value. When I initially assign an empty string or null value like <code>var str = null;</code> or <code>var str = " ";</code> then the function <code>function checkUrl(url){...}</code>.  returns that value which has been initialized initially. I don't know why its not getting initialized during run time when I get the response.</p>
  
  <p><strong>My Code</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function checkUrl(url){
  
     var str;
     $.ajax({
        method:"GET",
        url:"http://www.bitbaysolutions.com/connections.php?fbPostUrl=true",
        data:{
         url:url
        },
        success:function(r){
         console.log(r);
           switch(r){
            case "not_using_ext":{
             console.log("Script2 Says After Response : User is not using the extension ! ");
             str = "not_using_ext";
             break;
            }
            case "using_ext":{
              console.log("Script2 Says After Response : User is using the extension !");
                   str = "using_ext";
                 break;
            }
           }
        }
     });
     alert("Value of str is :  " + str);
     return str;
     
  }</code></pre>
  </div>
  </div>
  </p>