Dev Helpers
← Back

Truncate at a word boundary

Truncate at a word boundary using JavaScript.

Open this example in the interactive library

Source code

const text = "A reference for the print studio",
  limit = 17;
console.log(
  text.length <= limit
    ? text
    : text.slice(0, limit + 1).replace(/\s+\S*$/, "") + "…",
);

Sample output

A reference for…