| Viewed 191 times

How to Truncate String at a Given Length in Javascript?


I need a function that takes a string (the string to truncate) and a number (the maximum length of the truncated string) as arguments and returns the truncated string at the given length.

1 Answer
Mohammad Qandeel
Answered
15

use this function, it returns the truncated string at the given length.

truncate = (s, l) => s.substring(0, l > s.length ? l : s.lastIndexOf(' ', l))


Related Questions