JavaScript Trim string with String.prototype.trim() Method – Today I am going to teach you how you can trim a string using JavaScript. JavaScript offers a trim() method which helps you remove the white space from both the side in any string. As a developer, we might require to trim a string any time this is where trim() method comes in handy.

As we have known, the trim() method removes whitespaces from both ends of a string. When we talk about whitespace related to this context, this means the space, tab, no-break space, etc. and all the line terminator characters LF, CR, etc.

The string value is not affected by the trim() method and, It doesn’t take any argument whatsoever.

Check out the Javascript Trim Function Syntax

str.trim()

JavaScript offers 3 types of method to trim the string.
trim() – This method removes the characters from both the points of the string, moreover it doesn’t change the original string.

trimLeft() – This method removes the characters from the starting of the string.

trimRight() – This function removes the characters from the last point of the string or opposite to the trimLeft() method.

Javascript trim() Method

So far we’ve learned trim() method trims the string from both the side. This method is constructive when you need to remove the extra space entered by the users. Usually, users are not aware of the fact that they put extra space in the string.

Let’s say while logging in a user enters whitespace in his user name, he won’t be able to log in. This is where trim() method comes in handy.

let myString = "     HelloWorld       ";
console.log(myString.trim());

// Output: > HelloWorld

Now you can see trim() method returns HelloWorld string after trimming its both the sides.

Using jQuery trim() method

You can also use trim() method with jQuery to trim the string.

jQuery.trim('  Hello World   ');

Now let’s talk about browser compatibility issues Internet Explorer 8 or earlier versions. If your browser is not capable of the running trim() method, then you can use your custom trim() method to trim a string. Use the below custom code to remove whitespaces from both the sides of a string with the regular expression:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}

Javascript trimLeft() Method

Let us find out how does trimLeft() method help us to trim the string from the beginning point of a string.

let myString = "  Hello World";
myString.trimLeft();

// Output: Hello World

As you can see we’ve added white spaces from the starting point of the string. trimLeft() method returned a new sting excluding white space from the original string. Below you can also check the polyfill for trimLeft() method.

(function(w){
    var String=w.String, Proto=String.prototype;

    (function(o,p){
        if(p in o?o[p]?false:true:true){
            var r=/^\s+/;
            o[p]=o.trimLeft||function(){
                return this.replace(r,'')
            }
        
    })(Proto,'trimStart');

})(window); 

Javascript trimRight() Method

This function removes the whitespace from the right side of the string.

let myString = "Hello World  ";
myString.trimRight();

// Output: Hello World

We’ve completed JavaScript Trim String with String.prototype.trim() topic.