String.prototype.trimEnd
Removes whitespace and line breaks from the end of a string.
Available since: ES2019
Syntax
str.trimEnd()Return value
stringA new string without trailing whitespace.
Examples
Leading spaces stay:
console.log(`[${' Anna '.trimEnd()}]`);Output:
[ Anna]Removing a trailing line break:
const line = 'last line\n';
console.log(line.trimEnd().length);Output:
9Notes
Line breaks, tabs and non-breaking spaces also count as whitespace.
See also
- String.prototype.trim — Removes whitespace from both ends of a string.
- String.prototype.trimStart — Removes whitespace and line breaks from the start of a string.
Related recipes
In the tutorial
- String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.