JavaScript

How to Remove Extra Space Between Words in JavaScript

Spaces are used to connect words in a sentence that are separated from other words. It makes the entire sentence meaningful, readable, and easily understandable. Sometimes, there may be some extra spaces added in the text that may disturb the formatting. You must eliminate the additional spaces between the sentences in this situation.

This guide will illustrate the procedure to eliminate extra spaces between words.

How to Remove Extra Space Between Words in JavaScript?

For removing extra spaces between words, there are some JavaScript predefined methods listed below:

Let’s examine the working of each method separately.

Method 1: Remove Extra Space Between Words Using replace() Method

The “replace()” is a predefined method of the String type object used to replace the value in a string with the defined string, character, or any symbol. It returns a new string as output with the replaced values after searching the string for a specific value or a regex pattern.

Syntax

Follow the below-provided syntax for using the replace() method:

string.replace("searchValue", "replaceValue")

The replace() method accepts two parameters, one is the “searchValue” that will be searched and replaced, and the other is the “replaceValue” used as a replacer.

Example

In this example, first, we will create a variable named “string” that stores a string with extra spaces:

var string = "Welcome to LinuxHint";

Then, create a regex pattern and store it in a variable called “regexPattern”:

var regexPattern = /\s+/g;

Here, “\s” is used for the spaces, “+” sign indicates all the spaces in a string, and “g” is the global flag which is added to search the full string and identify spaces.

Now, invoke the “replace()” method by passing the regex pattern as a searchValue and the space as a replaceValue:

var ans = string.replace(regexPattern, " ");

Here, the pattern will search the spaces between words and replace them with a single space.

Finally, print the resultant string on the console using the “console.log()” method:

console.log(ans);

As you can see that the output gives a string with a single space between words:

If you want to remove all spaces between words, use the below method.

Method 2: Remove Extra Space Between Words Using split() Method

For removing extra spaces between words, utilize the “split()” JavaScript method with the “join()” method. The split() method divides the string into an array of substrings, while the join() method concatenates the substrings and outputs a new string. The split() method takes a parameter that needs to be replaced, and the join() method accepts the replacer as its argument.

This method finds the spaces in the string and replaces all the spaces with the empty string.

Syntax

Follow the below-given syntax for using the split() method:

string.split(" ").join("")

Here, the split() method splits the string based on the space and joins them with an empty string.

Example

We will now use the same string that is already created in the above example and call the “split()” method with the “join()” method:

var ans = string.split(" ").join("")

Lastly, print the resultant string on the console using the “console.log()” method:

console.log(ans);

The output shows that all the spaces between the string are successfully removed:

If you want to remove the spaces that occur at the beginning of the string, you have to follow the given section.

Method 3: Remove Extra Space Between Words Using trim() Method

The “trim()” method is another JavaScript method that is used to remove spaces. It can remove the spaces at the starting and ending positions of the string and gives a new trimmed string. It not only removes white spaces but also removes tabs and line breaks from a string. This method does not accept any parameter as an argument.

Syntax

Follow the below-mentioned syntax for removing extra spaces between words:

string.trim();

Example

Here, we will create a string leading extra spaces and store it in a variable named “string”:

var string = " Welcome to LinuxHint";

Then, call the “trim()” method that will remove the spaces from the start of the string:

var ans = string.trim();

Print the resultant string on the console:

console.log(ans);

The spaces from the beginning of the string are successfully removed:

We have compiled all the procedures for eliminating extra spaces between words.

Conclusion

For removing extra spaces, you can use the predefined JavaScript methods, including replace() method, split() method, or trim() method. The trim() method removes the spaces at the start of the string, while the replace() method removes extra spaces between words. The split() method removes all the spaces between words. This guide illustrated the procedure for eliminating extra spaces between words with detailed explanations.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.