title: Javascript if string contains a space replace the space with a dash
body: <p>I'm trying to write some code that will check if a previously defined variable has a space, and if it does, replace the space with a dash. This will be nested in another function.</p>
  
  <pre><code>function foo(){            
      // If stateName has a space replaces the space with a dash
  
      window.open('http://website.html/state-solar-policy/' + stateName);
  }
  </code></pre>