String.prototype.trimEnd()
Removes whitespace and line terminators from the end of a string.
trimEnd()
trimRight()- Added in JavaScript ES2019
Return value
stringA new string representing str stripped of whitespace from its end (right side). If the end of str has no whitespace, a new string is still returned (essentially a copy of str).
Changelog
- PHP ES2019Standardised as trimEnd, chosen for consistency with padEnd(). The earlier non-standard trimRight remains as an alias to the exact same function object, so String.prototype.trimRight.name === "trimEnd".
trimEnd() removes whitespace from the end of a string and leaves the beginning alone. It is the one you want when indentation is meaningful — stripping trailing spaces from a source line without destroying its indent, or dropping the newline from a line of piped input.
trimRight is the same function
Engines implemented the non-standard trimRight after trim() was standardised. When the method reached the specification it was named trimEnd to pair with padEnd(), and trimRight was kept as an alias to the identical function object for web compatibility. String.prototype.trimRight === String.prototype.trimEnd is true.
Common uses
Trailing whitespace is invisible and therefore poisonous: it breaks string equality, shows up as diff noise, and turns a "blank" line into a non-empty one. A per-line trimEnd() is the standard fix before writing a file. It is also the right way to remove a line's terminator when you are about to add your own — line.trimEnd() handles \n and \r\n alike, so you do not need a CRLF-aware regex.
Whitespace here means the same set trim() uses: Unicode space characters plus line terminators.
Example
When to use it
- Strip trailing whitespace from every line before writing a file
- Remove the line terminator from piped input, CRLF or LF
- Clean the end of a pasted value while keeping its indent
- Normalise lines before comparing two text blocks
Frequently asked questions
What is the difference between trimEnd and trimRight?
There is none — trimRight is a web-compatibility alias for the exact same function object as trimEnd, so the two are strictly equal and trimRight.name is "trimEnd". Use trimEnd, the standard name chosen to match padEnd().
How do I remove the trailing newline from a string?
str.trimEnd() removes it along with any other trailing whitespace, and handles \n and \r\n identically. If you must remove exactly one line terminator and keep the rest, use str.replace(/\r?\n$/, "").
Does trimEnd remove the indentation at the start of a line?
No. trimEnd() only touches the end of the string, so leading indentation is preserved exactly. Use trim() to strip both ends or trimStart() to strip only the beginning.
More examples
Stripping trailing whitespace from a file
Every line loses its invisible trailing spaces and tabs while the two-space indent on the return survives. This is the exact transformation most editors call "trim trailing whitespace on save".
const src = "function f() { \n return 1;\t\n} \n";
const cleaned = src
.split("\n")
.map((l) => l.trimEnd())
.join("\n");
console.log(JSON.stringify(src));
console.log(JSON.stringify(cleaned));It removes more than one newline
trimEnd collapses all three newlines, while the regex removes exactly one. When you are round-tripping a file and want to preserve deliberate blank lines at the end, the regex is the correct tool.
const s = "text\n\n\n";
console.log(JSON.stringify(s.trimEnd())); // "text"
console.log(JSON.stringify(s.replace(/\r?\n$/, ""))); // "text\n\n"
console.log(JSON.stringify("text\n\n\n".trimEnd() + "\n"));Detecting blank lines
The two whitespace-only lines report true because trimEnd reduces them to an empty string, while "code" and "more" report false. This is the cheap way to find lines that look blank in an editor but are not.
const lines = ["code", " ", "\t\t", "more"];
for (const l of lines) {
console.log(JSON.stringify(l), l.trimEnd() === "");
}
Discussion