title: Extract First n Words From String jQuery, Wordpress Style Excerpt In jQuery (x Number Of Words
body: <p>I have a little jQuery snippet that works, but in order to improve my JS, I'd like suggestions on how to improve its efficiency.</p>
  <p>This function shortens a WordPress Excerpt. It takes the original WordPress Excerpt and extracts the first 40 words, that's the part I care about. After that, it adds the 'Continue Reading' link at the end of the original Excerpt and adds it to the end of the truncated version.</p>
  <p>Again, I just wondered what would be a faster/more concise way to return the truncated excerpt. I tried 'slice' and got a bunch of commas.</p>
  <pre><code>jQuery('.page-id-7 .promo_slider_excerpt').each(function (i) {
      var s = jQuery(this).html();
      var sw = s.split(' '); // separate contents into array of words
      var t = [];
      var v;
      if (sw.length &gt; 40) {
          var a = jQuery(this).children('a'); // this is the Continue Reading link
          for (v = 0; v &lt; 40; v++) {
              t = (t + ' ' + sw[v]); // Build the shortened excerpt (this is the part I wanted to improve)
          }
          t = (t + ' ' + a[0].outerHTML); // Add the Continue Reading onto the end
          jQuery(this).html(t);
      } else {
          t = s;
          jQuery(this).html(t);
      }
  });
  </code></pre>