title: Shorten string without cutting words in JavaScript
body: <p>I'm not very good with string manipulation in JavaScript, and I was wondering how you would go about shortening a string without cutting any word off. I know how to use substring, but not indexOf or anything really well.</p>
  
  <p>Say I had the following string:</p>
  
  <pre><code>text = "this is a long string I cant display"
  </code></pre>
  
  <p>I want to trim it down to 10 characters, but if it doesn't end with a space, finish the word. I don't want the string variable to look like this:</p>
  
  <blockquote>
    <p>"this is a long string I cant dis"</p>
  </blockquote>
  
  <p>I want it to finish the word until a space occurs.</p>