limit words javascript code example
Example 1: javascript truncate string full word
const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;
// Example
truncate('This is a long message', 20, '...');
Example 2: Limit text to specified number of words using Javascript
"Want better search results? See our search tips".split(" ").splice(0,3).join(" ")