title: How do I return location.hostname without the trailing slash?
body: <p>How do I return the string <code>example.com</code> using JavaScript if the input is <code>https://example.com/page.html?query=string</code>?</p>
  
  <p>Right now, if I use <code>window.location.hostname</code> – or <code>location.hostname</code> – the return value is<br/><code>example.com</code><strong><em><code>/</code></em></strong>, but I want<br/><code>example.com</code>, without the trailing slash.</p>
  
  <hr>
  
  <p>This question is similar to @<a href="https://stackoverflow.com/u/3914662">r1853</a>’s “<a href="https://stackoverflow.com/questions/31185383">How to remove trailing slash from window.location.pathname</a>”, but because JavaScript provides many ways to grab parts of a URI (<code>location.hostname</code>, <code>location.host</code>, <code>location.href</code>), and because wellformed URIs are a closed class, I had assumed there was a less expensive option than using a regular expression to remove trailing slashes from them.</p>