title: How to get data from ajax response and assign to a javascript variable
body: <p>Below function always alerts <code>undefined</code>. Code execution is completed before the ajax request is completed. Kindly help .</p>
  
  <pre><code>function validateUser() {
      var user = document.getElementById("user").value
      var isuserValid = isvalidUser(user);
      alert("returned Result:" + isuserValid); // it always prints undefined,ajax request not completed.How can i wait for ajax request to complete 
      if (isuserValid) {
          alert("user Account found);
          } else {
              alert("Invalid user");
          }
      }
  
      function isValidUser(user) {
          $.ajax({
              url: 'validateUser.jsp'
              type: 'GET',
              dataType: 'html'
              data: {
                  userName: user
              },
              success: function(data) {
                  if (data.trim() = "true")
                      return true;
              } else {
                  return false;
              }
          });
      }
  
  //it always alerting undefined 
  </code></pre>