Skip to main content
How to Trim Whitespace from JavaScript String

How to Trim Whitespace from JavaScript String

This quick javascript tutorial helps to remove extra whitespaces including tabs, and line breaks from your data. We will explore various techniques and best practices for stripping whitespace in JavaScript.

The Context of Extra WhiteSpace:

Sometimes, We are getting data from user input and data has some extra space at the beginning or end of data.

Strip Whitespaces in JavaScript

We’ll also remove Whitespaces, spaces, tabs, and line breaks from the string by using JavaScript. Let’s demonstrate some examples of removing whitespace from a string. The following way I’ll discuss here is to js strip whitespace string.

  • Using trim() method
  • Using the regular expression method
  • Using split() and join()
  • replaceAll() Method

Removing Leading and Trailing Whitespace:

The trim() method is used to remove whitespace spaces, tabs, and newlines from the beginning and ending of a string. It does not change the original string. This can be useful when you want to clean up user input or remove unnecessary spaces from a string.

let s1 = " Hello, Adam! ";
// Using trim() to remove leading and trailing whitespace
let output_trim_string = s1.trim();

in the above trim() method is called on the s1 string, and it returns a new string with leading and trailing whitespace removed. Keep in mind that trim() does not modify the original string;

Output: "Hello, Adam!"

Using Regular Expressions:

We can also remove whitespace efficiently from the source string in JavaScript. Regular expressions are a powerful tool for pattern matching and manipulation in a staring. The Javascript replace() method can be used with a regex pattern to whitespace from the string:

let s1 = " Hello, Adam! ";
function removeWhitespace(inputString) {
  return inputString.replace(/\s+/g, ' ');
let result_string = removeWhitespace(s1);

Here, the replace method replaces an empty string for any whitespace characters (spaces, tabs, and line breaks) that are targeted by the regular expression /\s/g.

Output: "Hello, Adam!"

Split and Join Technique

We can also get the same result by using split() and join method(). First, The string will be divided into a variety of words, and then we’ll put them all back together without any whitespace.

let s1 = " Hello! I am Adam ";
let result = s1.split(' ').join('');
Output: "Hello!IamAdam"

ReplaceAll() Methods

The replaceAll is a built-in method that helps to replace all occurrences of a specified substring with another. This method introduced ECMAScript 2021.

function removeWhitespace(source_str) {
  let res = source_str.replaceAll(/\s+/g, ' ');
 return res;

let s1 = " Hello! I am Adam ";
let res = removeWhitespace(s1);
Output: "Hello! I am Adam"

The code snippet defines the removeWhitespace() method, which uses the replaceAll function with a regular expression (/\s+/g) to match one or more whitespace characters in the string globally. It then replaces these occurrences with a single space.


We have discussed different ways to remove whitespace in a string using JavaScript with methods like trim, replaceAll, and split/join. Choosing the appropriate method depends on specific needs.

Leave a Reply

Your email address will not be published. Required fields are marked *