Skip to main content

Command Palette

Search for a command to run...

How to truncate long-string in JavaScript

Published
•1 min read
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)

Thank you for reading, have a nice day!

More from this blog

D

Dhairya Shah

119 posts