Truncates a string to a specified length, appending a suffix if the string is too long.
The string to truncate
The maximum length of the resulting string
The suffix to append if truncation occurs
"..." Copy
"..."
The truncated string with the suffix if the original string exceeds the length, otherwise the original string.
console.log(truncate("hello world", 8)); // "hello..."console.log(truncate("hi", 5)); // "hi"console.log(truncate("hello", 5, "...")); // "hello"console.log(truncate("", 3)); // "" Copy
console.log(truncate("hello world", 8)); // "hello..."console.log(truncate("hi", 5)); // "hi"console.log(truncate("hello", 5, "...")); // "hello"console.log(truncate("", 3)); // ""
Truncates a string to a specified length, appending a suffix if the string is too long.