Truncate a String
Shortens a string to a specified maximum length and appends an ellipsis if truncation occurs.
truncateString
The truncateString function shortens a string to a specified maximum length and appends an ellipsis (...) or a custom suffix if it exceeds that length.
Function Signature
function truncateString(str: string, maxLength: number): string
function truncateString(str: string, options?: TruncateOptions): stringParameters
str(string): The string to truncate.maxLength(number): The maximum allowed length before truncation. Defaults to100.options(TruncateOptions, optional): Configuration options:maxLength(number): The maximum allowed length before truncation. Defaults to100.suffix(string): The suffix to append to the truncated string. Defaults to'...'.trim(boolean): Whether to trim all whitespace characters before truncating. Defaults tofalse.
Return Value
string: The truncated string based on the options or the original string if its length is less than or equal tomaxLength.
Example Usage
playground.ts
Last updated: Sun, Jun 28, 2026 08:49:40AM (UTC)
