Truncate

Truncates a string to a specified length.

Contributed by @itsbrunodev

javascript
function truncate(str, num) {
  return str.length > num ? str.slice(0, num) + "..." : str;
}
javascript
truncate("Hello World", 5); // "Hello..."

Keywords

string
truncate