title: Why I cant JSON.parse a string from an API (Error msg: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>))
body: <p>I am trying to fetch a joke using XMLHTTPRequest from a random joke generating API as practice.</p>
  <p>Here is my code</p>
  <pre><code>    &lt;script&gt;
      const xhr = new XMLHttpRequest();
  
      xhr.open('get', 'https://icanhazdadjoke.com/', true);
      xhr.setRequestHeader('Accept', 'application/json')
          const joke = JSON.parse(xhr.response)
  
      xhr.onload = function () {console.log(xhr.responseText)}
      xhr.send();
  
  &lt;/script&gt;
  </code></pre>
  <p>and the error message i get is:  Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse ()</p>
  <p>Funny thing is if I do JSON.parse(xhr.response) and save it to a variable, it works perfectly fine. What am I doing wrong in my script?</p>