Categories
JavaScript Answers

How to apply trim function to each string in an array with JavaScript?

Spread the love

Sometimes, we want to apply trim function to each string in an array with JavaScript.

In this article, we’ll look at how to apply trim function to each string in an array with JavaScript.

How to apply trim function to each string in an array with JavaScript?

To apply trim function to each string in an array with JavaScript, we can use the array map method.

For instance, we write

const trimmedArr = arr.map((s) => s.trim());

to call arr.map with a callback that returns the trimmed version of each string we get from calling s.trim.

And then we assign the returned array to trimmedArr.

Conclusion

To apply trim function to each string in an array with JavaScript, we can use the array map 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 *