Remove Whitespace from Start or End of String with JavaScript?

To remove the whitespace from the start and end of a string, call the String.trim() method. The String.trim() method returns a new string with all leading and trailing whitespace characters such as spaces, tabs, and line breaks removed from the string.

The trim() method does not modify the original string, instead, it returns a new string without any leading or trailing white space characters.

The following example trims white space from both ends of str:

let str = "  Hello, World!  ";
let trimmedStr = str.trim();

console.log(trimmedStr); 
// Output: "Hello, World!"

Remove White Space from the Beginning of a String

If you want to remove the white space from the beginning of the string only without caring about the end of the string, you can use the String.trimStart() method.

The trimStart() method returns a new string with the leading whitespace characters removed. Just like the trim() method, it also doesn’t modify the original string.

The following example removes white spaces from the beginning of the string:

let str = "  Hello, World!  ";
let trimmedStr = str.trimStart();

console.log(trimmedStr); 
// Output: "Hello, World!  "

You can also use the trimLeft() method to remove the white space characters from the beginning of a string.

The trimLeft() method is an alias of the trimStart() method. It works the same way as the trimStart() method.


Remove White Space from the End of a String

To remove the white space characters from the end of a string you can use the String.trimEnd() method.

The trimEnd() method works just like the trimStart() method. The only difference is that it removes the white spaces from the end of the string instead of the beginning.

Note that the original string is not modified with either method.

The following example removes the white spaces from the end of the string:

let str = "  Hello, World!  ";
let trimmedStr = str.trimEnd();

console.log(trimmedStr); 
// Output: "  Hello, World!"

You can also use the trimRight() method to remove white spaces from the end of the string.

The trimRight() method is an alias of the trimEnd() method. It works the same way as the trimEnd() method.

The trimRight() method was introduced as a non-standard method in some browsers before the ES2019 specification introduced the trimEnd() method.

The trimRight() is still supported in some browsers as an alias for the trimEnd() method, but it’s recommended to use the standard method to ensure cross-browser compatibility.


Trimming Whitespace with Regular Expression

Another way to remove whitespace from a string is to use the replace() method with a regular expression.

A regular expression is a pattern that can match various types of strings, including whitespace characters.

The following example uses the replace() method to trim the white spaces:

// Original string
let str = "   Hello World!   ";

// Remove white spaces from start and end
let trimmedStr = str.replace(/^\s+|\s+$/g, '');

console.log(trimmedStr); 
// Output: "Hello World!"

In the above example, the regular expression /^\s+|\s+$/g matches one or more whitespace characters (\s+) at the beginning (^) or end ($) of the string, and the g flag tells it to match globally throughout the string.

The second argument of replace() is an empty string, which replaces the matched whitespace characters with nothing.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts