title: How can I use Global variable across multiple functions in JavaScript?
body: <p>I have a piece of code which is intended to check the permission level and group membership of a use and launch a dialog box if the user has the correct permissions to access that section of the site.</p>
  
  <pre><code>function bindSettingsButton() {
              $("#mt-ngw-personalsettings").on("click", function() {
                  RequestNewSite();
              });
          }
  
          function RequestNewSite()   {
              var HasPermission = false;
              var isGroupMember = false;
              CheckCurrentUserMembership();
              CheckUserHasEditPermissions();
              CheckUserPermissions();
          }
  
          function CheckCurrentUserMembership() {
              var clientContext = new SP.ClientContext.get_current();
              this.currentUser = clientContext.get_web().get_currentUser();
              clientContext.load(this.currentUser);
  
              this.userGroups = this.currentUser.get_groups();
              clientContext.load(this.userGroups);
              clientContext.executeQueryAsync(OnQuerySucceeded, OnQueryFailed);
          }
  
          function OnQuerySucceeded() {
              var isMember = false;
              var groupsEnumerator = userGroups.getEnumerator();
              while (groupsEnumerator.moveNext()) {
                  var group = groupsEnumerator.get_current();
                  if(group.get_title() == "Create Site OptOut") {
                  isMember = true;
                  this.isGroupMember = true;
                  break;
                  }
              }
          }
  
          function OnQueryFailed() 
          {
              alert("Couldn't check user group membership. Please contact  to resolve this issue.");
          }
  
          function CheckUserHasEditPermissions()  {
              context = new SP.ClientContext.get_current();
              web = context.get_web();
              this._currentUser = web.get_currentUser();
              this._theList = web.get_lists().getByTitle('siterequests');
              context.load(this._currentUser);
              context.load(this._theList, 'EffectiveBasePermissions')
              context.executeQueryAsync(Function.createDelegate(this, this.onPermissionsSuccessMethod), Function.createDelegate(this, this.onPermissionsFailureMethod));
          }
  
          function onPermissionsSuccessMethod(sender, args)   {
              if (this._theList.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems))
              {
                  this.HasPermission = true;
              }
              else
              {
                  this.HasPermission = false;
              }
          }
  
          function onPermissionsFailureMethod()
              {
                  alert("Couldn't check permissions. Please contact  to resolve this issue.");
              }
  
          function CheckUserPermissions() {
              if(this.isGroupMember == true)
              {
                  alert("You do not have permission to create sites. If you believe you should have access to this functionality, please contact .");
              }
              else if(this.HasPermission == false)
              {
                  alert("You do not have permission to create sites. If you believe you should have access to this functionality, please contact .");
              }
              else
              {
                  showDialogue();
                  document.getElementById("next-stage").focus();
              }
          }
  </code></pre>
  
  <p>Unfortunately when it reaches the end this section the variables HasPermission and isGroupMember are still undefined so the dialogue launches automatically for every user.
  I have a feeling I have misused the .this keywords and this is a scoping error but I am not expert enough in JS to know for certain or be able to fix it. Can anyone tell me exactly what I've done wrong and how to fix it please?</p>