How To Remove Spaces In Javascript

Spaces separate words, they make words a sentence and help us read the words and understand them better. But sometimes there are extra spaces and you might want to remove them. For instance, the contact for users filled out had extra white spaces in all of the fields. So, before entering it into the database you would want that extra white space to be removed. Similarly, there could be any situation where you would want to remove the spaces. So, in this article, we are going to discuss how you can remove spaces in Javascript. 

Removing unwanted white spaces in JavaScript comes with some methods that are predefined. So, all you have to do is call and use them. Following are the methods any Javascript developers for hire can use.

.replace()

This method is of String type objects, it comes predefined and helps replace a value or regex pattern defined to it with some value in the string. As a result, it returns a new string with the values removed. The way it works is by searching the specified value or RegEx here is how you can use it.

Javascript developers can pass two parameters to the replace() method. The first is the value to be checked for and the second is the value to be replaced with. You can pass variables or lists where the search and replace values are stored or directly pass them as is to the parameter. 

let string = “Welcome to LinuxHint”;

var regexPattern = /\s+/g;

let ans = string.replace(regexPattern, “+”);

console.log(ans);

To know more about regex patterns and how they work check out the link. Here is an explanation of the above code a little so you could understand it better:

“\s” searches for spaces, “+” sign checks for all the spaces in any string. “g” is a regex 

global flag, it is added for searching entire strings.

RegexPattern is a variable where the regex pattern is specified, it searches for the spaces. The second argument is the “+” so it replaces the searches found with “+”

Remove all space 

There are two methods to remove all the spaces between words. The first is by using the .replace() method with a slightly different approach. 

.replace()

Here Javascript developers for hire can use the same code as above but this time the difference. Instead of the “+” symbol pass an empty string as a replacement value. So when the method will go to replace values it will replace it with nothing, i.e. “”

Here is the code to help you understand. 

let string = “Welcome to LinuxHint”;

var regexPattern = /\s+/g;

let ans = string.replace(regexPattern, “”);

console.log(ans);

.split()

The other method is using the split method in combination with the join method. Split divides the strings into arrays depending on the spaces between them. Then the join method will join those array elements together and give the new string.

split() takes one parameter for replacement, join() takes one argument to replace the values with.

let string = “Welcome to LinuxHint”;

var ans = string.split(” “).join(“”)

console.log(ans);

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

trim()

trim() is another method that Javascript developers use to remove spaces often. But the only thing with trim is that it removes the space only from the beginning of the string. 

It does not accept any parameters, and here is how to use it.

Let str = ”  abc  def”;

var ans = str.trim();

console.log(ans);

Conclusion

Depending on the way you want to remove spaces you can choose the method. Now you can also ask these questions when you want to hire JS developers. If you are searching to hire JS developers then contact vteams and hire remote developers now. 

Published by cengizcoskuno

I write my blogs regularly. After visiting my blog you can find out many articles on different topics. You should read those articles as they are written for readers like you.

Leave a comment

Design a site like this with WordPress.com
Get started