DEV Community

Cover image for How to truncate long-string in JavaScript
Dhairya Shah
Dhairya Shah

Posted on • Originally published at codewithsnowbit.hashnode.dev

How to truncate long-string in JavaScript


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)
Enter fullscreen mode Exit fullscreen mode

Thank you for reading, have a nice day!

Discussion (0)