How to truncate long-string in JavaScript

How to truncate long-string in JavaScript

ยท

1 min read

It becomes a hassle when working with a too-long string, don't worry, today, in this article I will be showing an easy to come out from that hassle by trimming/truncating the string.

const truncate = (str, num) => {
    return str.length > num ? str.slice(0, num) + "..." : str;
}
const str = "the quick brown fox jumps over the lazy dog"
truncate(str, 18)

Thank you for reading, have a nice day!

ย