How to remove spaces from a string in javascript
anjan-dutta

anjan-dutta @anjandutta

About: Welcome to my page! I am Anjan, a front-end developer from India. In my blog, I write about Javascript and front-end programming articles to help other developers.

Joined:
May 24, 2021

How to remove spaces from a string in javascript

Publish Date: May 25 '21
4 0

Space can be present at any position in a string and, a single solution will not work in every situation.

In this article, I have discussed two ways to:

  • Trim the extra space from the beginning or end of a string.
  • Remove all extra spacing between words.

Trim the extra space from the beginning or end of a string.

Javascript has an inbuild trim() function for this purpose. The trim function takes one parameter, the string itself and, it returns a trimmed string.

The syntax looks like below:

var content = "      The Quick!       ";
console.log(content.trim());
//The Quick!
Enter fullscreen mode Exit fullscreen mode

Simple, right!

Next, you can read how to remove all extra spacing between words from here in my original blog post

Comments 0 total

    Add comment