JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to use the String trimEnd() method in JavaScript?

If you want to remove the whitespaces from the end of a string, then use the trimEnd() method. The trimEnd() method does not modify the original string and returns a new string that is right-trimmed.

The syntax for the trimEnd() method is:

let rightTrimmedString = originalString.trimEnd();

In JavaScript, the following characters are considered whitespaces:

  • Spaces
  • Tabs
  • Newline
  • Carriage return
  • Form feed

The following example explains how to use trimEnd() method.

let str = "   JavaScript String     ";
let rightTrimmedString = str.trimEnd();

console.log('Right-trimmed String');
console.log(rightTrimmedString);
console.log('Original String');
console.log(str);

Output

Right-trimmed String
"   JavaScript String"
Original String
"   JavaScript String     "

Recommended Posts