DEV Community

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

Posted on • Edited on • Originally published at codewithsnowbit.hashnode.dev

4 3

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!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay