truncate

Truncates a string to a specified maximum length and appends an ellipsis if the string is longer than the maximum length.

truncate()

  • Receives the maximum length of the truncated string. Default is 200.

  • Returns the truncated string with an ellipsis if the original string was longer than the maximum length.

const originalString = 'This is a very long string that needs to be truncated';
const truncatedString = originalString.truncate(30);
console.log(truncatedString); // Outputs: 'This is a very long string tha...'

Last updated