JavaScript String trimEnd

Summary: in this tutorial, you’ll learn how to use the JavaScript String trimEnd() method to remove whitespace characters from the end of a string.

To remove the whitespace characters from the end of a string, you use the trimEnd() method:

let newString = originalString.trimEnd();
Code language: JavaScript (javascript)

The trimEnd() method returns a new string from the original string with the ending whitespace characters stripped. The trimEnd() method doesn’t change the original string. The following characters are the whitespace characters in JavaScript:

  • A space character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character

The following example shows how to use the trimEnd() to remove the whitespace characters from the end of a string:

const str = '   JavaScript   ';
const result = str.trimEnd();

console.log({ str });
console.log({ result });
Code language: JavaScript (javascript)

Output:

{ str: '   JavaScript   ' }
{ result: '   JavaScript' }
Code language: JavaScript (javascript)

The trimRight() method is an alias of the trimEnd() method. The trimRight() provides the same functionality as the trimRight() method. However, it’s recommended that you use the trimEnd() method.

Summary

  • The trimEnd() method returns a new string from an original string with the ending whitespace characters stripped. The trimEnd() method doesn’t change the original string.
  • The trimRight() method is an alias of the trimEnd() method.
Was this tutorial helpful ?