Categories
JavaScript Answers

How to display only 10 characters of a long string with JavaScript?

Spread the love

Sometimes, we want to display only 10 characters of a long string with JavaScript.

In this article, we’ll look at how to display only 10 characters of a long string with JavaScript.

How to display only 10 characters of a long string with JavaScript?

To display only 10 characters of a long string with JavaScript, we can use the string slice method.

For instance, we write

const example = "I am too long string";
const result = example.slice(0, 10) + "...";

to call example.slice with 0 and 10 to return a string with the first 10 characters in example.

Then we add the '...' string after than and assign the combined string to result.

Conclusion

To display only 10 characters of a long string with JavaScript, we can use the string slice method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *