Remove Whitespace from JavaScript String

In this post you will learn how to remove whitespace from a javascript string.

In JavaScript it's very easy and simple to remove whitespace. We can remove leading whitespace and trailing whitespace using trimStart() and trimEnd(). Or if you want to remove all white space use trim().

Before proceeding, let us understand what whitespace in javascript means.

What is Whitespace?

Whitespace are spaces created when we use :

  • space
  • tab
  • line terminator characters
  • no-break space

Now let us learn the different ways to remove whitespace from a string.

Remove Whitespace from beginning and end of a string

To remove whitespace from the start and end of the string we can just use the Stirng.trim() method in JavaScript. It will remove all the spaces from either side of the string.

Let's see an example:

const string = " hello ";
const trimString = string.trim()
console.log(trimString) // "hello"

Note : trim() do not accept any parameters, it is use just to remove whitespace before and after a string.

Remove whitespace from beginning of a string.

To remove the leading whitespace from a string we have to use the String.trimStart() method. It will remove all the white spaces from the beginning of the string.

Lets see trimStart() in action:

const string = "   hello";
const trimString = string.trimStart()
console.log(trimString) // "hello"

Alternatively, you can also use trimLeft() method, it does the same thing, remove white space from the start of a string.

const string = "   hello";
console.log(string.trimLeft()) // "hello"

Remove whitespace from the end of a string

The String.trimEnd() method is use to remove the trailing white space in JavaScript. It remove all the white spaces from the end of the string.

Alternatively, you can use the String.trimRight(). It's an alias to trimEnd() method.

Let's see trimEnd() and trimRight() in action:

const string = "hello   ";
console.log(string.trimEnd()) // "hello"
console.log(string.trimRight()) // "hello"

Note : According to ECMAScript, both trimLeft() and trimRight() are use for compatibility of old codes.

Remove all whitespace between string in JavaScript

To remove all whitespaces between all characters in a string we have to use the combination of the split() and join() method.

The split() method breaks a string against a given regular expression and join() concatenate the elements and create a new string.

Let us see an example to understand it better.

const string = " Hello World !   "
const trimString = string.split(' ').join('')

console.log(trimString)  // "HelloWorld!"

Or we can use String.replace() method. It will search for white spaces using regular expression and replace them with an empty string('').

Example with replace():

const string = " Hello World !   "
let trimString = string.replace(/\s+/g, '');

console.log(trimString)  // "HelloWorld!"

The /\s+/g regex pattern will identify white spaces in the string and replace() will remove it with the empty string('').

Related Topics:

String Interpolation In JavaScript

Truncate A String In JavaScript With Example

Convert An Array To String In Javascript

Get The 10 Characters From A String Using Javascript.

Convert String To Float In JavaScript

Check If A String Contains A Substring In Javascript

💾 Recommended Articles