Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String trimEnd() and trimRight() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 at the end. 

The below example illustrates this method:

Example: This example describes the trimEnd() method. 

Javascript




function trimString() {
    str1 =
        " GeeksforGeeks     ";
    str2 =
        " Hello There! ";
 
    trimmed_out =
        str1.trimEnd();
    trimmed_out2 =
        str2.trimEnd();
 
    console.log('"' + trimmed_out + '"');
    console.log('"' + trimmed_out2 + '"');
}
trimString()

Output

" GeeksforGeeks"
" Hello There!"

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 spaces at the end. 

The below example illustrates this method:

Example: This example describes the trimRight() method. 

Javascript




function trimString() {
    str1 =
        " GeeksforGeeks     ";
    str2 =
        " Portal ";
 
    trimmed_out =
        str1.trimRight();
    trimmed_out2 =
        str2.trimRight();
 
    console.log('"' + trimmed_out + '"');
    console.log('"' + trimmed_out2 + '"');
}
trimString()

Output

" GeeksforGeeks"
" Portal"

We have a complete list of Javascript Strings, to check those please go through the Javascript String Complete reference article.

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

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

My Personal Notes arrow_drop_up
Last Updated : 29 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials