Truncate a string to a number of words in JavaScript

function truncateWords(text, limit) {
    var words = $.trim(text).split(/s+/);

    if (words.length <= limit) {
        return text;
    }

    return words.slice(0, limit).join(' ') + '...';
}