Truncate String

Truncate a string to a specified length with ellipsis.

Code

General
str.length > length ? str.slice(0, length - 3) + '...' : str;

Parameters

The string to truncate

Maximum length of the result

Browser·fetch() may be limited by CORS

More JavaScript Snippets