Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript String trimEnd() and trimRight() Method

JavaScript String trimEnd() and trimRight() Method

This article demonstrates trimEnd and trimRight methods of JavaScript string. Both methods are used to perform the same task and their implementation is shown below:

JavaScript trimEnd() 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 at the end. 

The below example illustrates this method:

Example: This example describes the trimEnd() method. 

Javascript




// Funciton to implement trimEnd method
function trimString() {
     
    // Input strings
    str1 = " neveropen     ";
    str2 = " Hello There! ";
 
    // Implementing trimend method
    trimmed_out = str1.trimEnd();
    trimmed_out2 = str2.trimEnd();
 
    // Display output
    console.log('"' + trimmed_out + '"');
    console.log('"' + trimmed_out2 + '"');
}
 
// Function call
trimString();


Output

" neveropen"
" Hello There!"

JavaScript trimRight() method

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 to implement trimRight method
function trimString() {
 
    // Imput Strings
    str1 = " neveropen     ";
    str2 = " Portal ";
 
    // Implementing trimRight method
    trimmed_out = str1.trimRight();
    trimmed_out2 = str2.trimRight();
 
    // Display output
    console.log('"' + trimmed_out + '"');
    console.log('"' + trimmed_out2 + '"');
}
 
// Function call
trimString();


Output

" neveropen"
" 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
  • Firefox
  • Edge
  • Safari
  • Opera
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments