Categories
JavaScript Answers

How to remove strings in beginning and end with JavaScript?

Spread the love

Sometimes, we want to remove strings in beginning and end with JavaScript.

In this article, we’ll look at how to remove strings in beginning and end with JavaScript.

How to remove strings in beginning and end with JavaScript?

To remove strings in beginning and end with JavaScript, we can use the string trim method.

For instance, we write:

const s = '  foo  '
const trimmed = s.trim()
console.log(trimmed)

to call s.trim to return a string without the whitespaces at the beginning and the end of s.

Therefore, trimmed is 'foo'.

Conclusion

To remove strings in beginning and end with JavaScript, we can use the string trim 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 *