JavaScript | trimEnd() and trimRight() Method

The trimEnd() method in JavaScript is used to remove white-space from the end of a string. The value of the string is not modified in any manner, including any white-space present before the string.

Syntax:

string.trimEnd()

Return Value: It returns the final string that is stripped out of all the white-space in the end.

Below example illustrates this method:

Example: This example describes the trimEnd() method.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | trimEnd() method
    </title>
</head>
  
<body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
    <b>
      JavaScript | trimEnd() method
    </b>
    <pre>Output for " GeeksforGeeks     " : 
      <span class="output"></span>
    </pre>
    <pre>Output for " Hello There!   " : 
      <span class="output_2"></span>
    </pre>
  
    <button onclick="trimString()">
        Trim End
    </button>
    <script type="text/javascript">
        
        /* main function */
        function trimString() {
            str1 =
                " GeeksforGeeks     ";
            str2 =
                " Hello There! ";
  
            trimmed_out =
                str1.trimEnd();
            trimmed_out2 =
                str2.trimEnd();
  
            document.querySelector('.output').textContent
                    = '"' + trimmed_out + '"';
            document.querySelector('.output_2').textContent
                    = '"' + trimmed_out2 + '"';
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    trimEnd-before
  • After clicking the button:
    trimEnd-after

The trimRight() alias, the trimEnd() method has an alias which is the trimRight() method. It performs exactly the same function as trimEnd().

Syntax:

string.trimRight()

Return Value: It returns the final string that is stripped out of all the white-space in the end.

Below example illustrates this method:
Example: This example describes the trimRight() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | trimRight() method
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        JavaScript | trimRight() method
    </b>
    <pre>Output for " GeeksforGeeks  " : 
    <span class="output"></span>
    </pre>
    <pre>Output for " Portal " : 
    <span class="output_2"></span>
    </pre>
  
    <button onclick="trimString()">
        Trim Right
    </button>
    <script type="text/javascript">
      
        /* Main function */
        function trimString() {
            str1 =
                " GeeksforGeeks  ";
            str2 =
                " Portal ";
  
            trimmed_out =
                str1.trimRight();
            trimmed_out2 =
                str2.trimRight();
  
            document.querySelector('.output').textContent
                    = '"' + trimmed_out + '"';
            document.querySelector('.output_2').textContent
                    = '"' + trimmed_out2 + '"';
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    trimRight-before
  • After clicking the button:
    trimRight-after

Supported Browsers: The browser supported by trimEnd() and trimRight() method are listed below:

  • Google Chrome 60
  • Firefox 61
  • Edge 12
  • Safari 12
  • Opera 53

full-stack-img




My Personal Notes arrow_drop_up

Im listening

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.