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:
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 "