So I have this function and I want the program to wait for it to return a value instead of giving undefined.
function savedNumberOfLessonInDay(dayID){
     var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
            document.getElementById('demo4').innerHTML = this.responseText; //Returns the right number
            return this.responseText;
        }
      };
      var PageToSendTo = "AJAX/countLessonInDay.php?";
    var MyVariable = dayID;
    var VariablePlaceholder = "GETDayID=";
    var UrlToSend = PageToSendTo + VariablePlaceholder + MyVariable;
      xhttp.open("GET", UrlToSend, false);
      xhttp.send();
   }
<?php
require '../notWebsite/dbh.php';
session_start();
$dayID = (int)$_GET['GETDayID'];
$sqlCount = "SELECT COUNT(lessonID) FROM daylesson WHERE dayID = ?";
  $stmt = mysqli_stmt_init($conn);
  if(!mysqli_stmt_prepare($stmt, $sqlCount)) {
   header("Location: ../GymnasieArbeteHemsida.php?error=countError");
    exit();
  }
  else {
 mysqli_stmt_bind_param($stmt, "i", $dayID);//Puts in variable in question
   mysqli_stmt_execute($stmt);//Executes the question
mysqli_stmt_bind_result($stmt, $result);//Binds the reuslt of the question to the variable $result
if(mysqli_stmt_fetch($stmt)){//If the result of the question can be recieved
  echo $result;//Send the result to the website
}
        exit();
  }
  mysqli_stmt_close($stmt);
  mysqli_close($conn);
 ?>
  document.getElementById('demo').innerHTML = 'test' + savedNumberOfLessonInDay(number);
This code returns testundefined to demo but 16(which is the number I'm after) to demo4. How do I make it returns test16 to demo instead of testundefined?
 
     
     
    