transformation
String.prototype.trimEnd
Returns a new string with trailing whitespace removed. Added in ES2019 (aliased as trimRight for legacy code).
str.trimEnd() Parameters
| Parameter | Purpose |
|---|---|
| (no arguments) | Method takes no parameters |
| returns | New string with trailing whitespace stripped |
Examples
console.log(' hello '.trimEnd()); Prints ' hello'
console.log('abc\n\t '.trimEnd()); Prints 'abc'
console.log(' abc'.trimEnd()); Prints ' abc'
console.log('hi '.trimEnd().length); Prints 2
Gotcha
trimRight is a legacy alias — prefer trimEnd. Interior and leading whitespace are untouched.
Related methods
String.prototype.trim
Returns a new string with whitespace removed from both ends. Whitespace includes spaces, tabs, newlines, and Unicode whitespace like non-breaking space.
String.prototype.trimStart
Returns a new string with leading whitespace removed. Added in ES2019 (aliased as trimLeft for legacy code).
String.prototype.padEnd
Returns a new string padded on the right with padString (default ' ') until it reaches targetLength. Added in ES2017.
String.prototype.replace
Returns a new string with the first match of pattern replaced. Pattern can be a string (only the first occurrence) or a RegExp (all occurrences when the g flag is set).
String.prototype.replaceAll
Returns a new string with every occurrence of pattern replaced. Added in ES2021; if pattern is a RegExp it must have the g flag or replaceAll throws TypeError.