title: Why prompt returns sometimes null as string
body: <p>a friend of mine asked me why this code doesn't work properly by canceling prompt.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var name = prompt("Enter your name:", "");
      if (!name) {
          name = "unknown";       
      }
      var ans =("&lt;h2&gt;" + "Hello, "+ name + "!"+"&lt;/h2&gt;");      
      document.write(ans);        
  &lt;/script&gt;
  </code></pre>
  
  <p>Here is jsfidle <a href="https://jsfiddle.net/085svr3u/" rel="nofollow">https://jsfiddle.net/085svr3u/</a></p>
  
  <p>I found out, that in the example above propmt returns "null" ie. null as string by cancel. And I have no idea why.</p>
  
  <p>I tried to play with original code and refactored it a bit. Now it works as expected. But I still have no explanation.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      function test() {
          var name = prompt("Enter your name:", "");
          if (!name) {
              name = "unknown";       
          }
          var ans =("&lt;h2&gt;" + "Hello, "+ name + "!"+"&lt;/h2&gt;");      
          document.write(ans);        
      }
  
      test();
  &lt;/script&gt;
  </code></pre>
  
  <p>And here is modified version <a href="https://jsfiddle.net/085svr3u/1/" rel="nofollow">https://jsfiddle.net/085svr3u/1/</a></p>
  
  <p>Thank you in advance.</p>