Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String trim() Method

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 04 Jan, 2023
Improve Article
Save Article
Like Article

The JavaScript String trim() method is used to remove the white spaces from both ends of the given string. 

There are two more functions that can be used to trim any particular end of any string JavaScript str.trimLeft() Function & JavaScript str.trimRight() Function.

Syntax:

str.trim()

Parameter: This method does not accept any parameter.

Return value: This method returns a new string, without any of the leading or trailing white spaces. 

Below are examples of the String trim() Method. 

Example: In this example, the trim() method removes all the leading and trailing spaces in the string str.

JavaScript




<script>
    function func() {
        var str = "GeeksforGeeks     ";
        var st = str.trim();
        console.log(st);
    }
    func();
</script>


Output:

GeeksforGeeks

Example 2: 

JavaScript




<script>
    function func() {
      
        // Original string containing whitespace
        var str = "   GeeksforGeeks";
      
        // Trimmed string
        var st = str.trim();
        console.log(st);  
    }
    func();
</script>


Output: 

GeeksforGeeks

JavaScript str.trimLeft(): The str.trimLeft() method is used to remove the white spaces from the start of the given string. It does not affect the trailing white spaces. 

Codes for the above method are provided below:

Example 1:  In this example, the extra space is placed on the right side of the string the trimLeft() function will work in this case.

JavaScript




<script>
    function func() {
      
        // Original string containing whitespace
        var str = "GeeksforGeeks      ";
      
        // Trimmed string
        var st = str.trimLeft();
        console.log(st);  
    }
      
    func();
</script>


Output: 

GeeksforGeeks      

Example 2: In this example, the extra space is placed on the left side of the string the trimLeft() function will work in this case.

JavaScript




<script>
    function func() {
      
        // Original string containing whitespace
        var str = "   GeeksforGeeks";
      
        // Trimmed string
        var st = str.trimLeft();
        console.log(st);  
    }
    func();
</script>


Output: 

GeeksforGeeks

JavaScript str.trimRight(): The str.trimRight() method is used to remove the white spaces from the end of the given string. It does not affect the white spaces at the start of the string.

Codes for the above method are provided below:

Example 1:  In this example, the extra space is placed on the right side of the string the trimRight() function will work in this case.

JavaScript




<script>
    function func() {
      
        // Original string containing whitespace
        var str = "GeeksforGeeks      ";
      
        // Trimmed string
        var st = str.trimRight();
        console.log(st);  
    }
    func();
</script>


Output:

GeeksForGeeks

Example 2: In this example, the extra space is placed on the left side of the string the trimRight() function won’t work in this case.

JavaScript




<script>
    function func() {
      
        // Original string containing whitespace
        var str = "   GeeksforGeeks";
      
        // Trimmed string
        var st = str.trimRight();
        console.log(st);  
    }
      
    func();
</script>


Output:

   GeeksforGeeks

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browser:

  • Chrome 4 and above
  • Edge 12 and above
  • Firefox 3.5 and above
  • Internet Explorer 10 and above
  • Opera 10.5 and above
  • Safari 5 and above

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!