String.prototype.trimEnd()
trimEnd() string · Added in vES2019 · Updated March 16, 2026 · String Methods The trimEnd() method removes whitespace characters from the end of a string and returns a new string. It’s the standard ES2019 method for right-side trimming, preferred over the legacy trimRight() alias.
Why trimEnd() Over trimRight()?
ES2019 introduced trimEnd() (and trimStart()) as the canonical names, aligning with other string methods like padEnd() and padStart(). The older trimRight() and trimLeft() aliases exist only for backward compatibility. Use trimEnd() in new code for:
- Consistency with
padEnd(),padStart() - Clarity — “End” explicitly means the right side, while “Right” could be confused with direction in right-to-left languages
- Future-proofing — legacy aliases may be deprecated in future ECMAScript versions
console.log("test".trimEnd === "test".trimRight); // true (same method)
String Immutability
JavaScript strings are immutable — they never change in place. trimEnd() returns a new string, leaving the original untouched:
const original = "hello ";
const trimmed = original.trimEnd();
console.log(original); // "hello " (unchanged)
console.log(trimmed); // "hello" (new string)
This applies to all string methods. Always capture the returned value.
What Gets Removed?
trimEnd() removes trailing whitespace, including:
- Spaces (
) and tabs (\t) - Newlines (
\n,\r\n,\r) - Non-breaking spaces (
\u00A0, common in HTML) - Other Unicode whitespace characters
Only the end is trimmed — characters in the middle or beginning remain.
const s = " middle \n";
s.trimEnd(); // " middle \n" — newline at end removed
s.trim(); // "middle" — whitespace removed from both ends
Use Cases
Cleaning file lines
When reading text files, each line often has accidental trailing whitespace:
const lines = [
"header ",
"data one ",
"data two\t",
"footer "
];
const cleaned = lines.map(line => line.trimEnd());
console.log(cleaned);
// ["header", "data one", "data two", "footer"]
Sanitizing user input
Form fields and API responses frequently contain padded whitespace:
function normalizeInput(value) {
return value.trimEnd();
}
console.log(normalizeInput("username ")); // "username"
console.log(normalizeInput("comment\t\n")); // "comment"
Preparing for comparison
Trailing whitespace causes subtle bugs in equality checks:
const stored = "password";
const input = "password ";
if (input.trimEnd() === stored) {
console.log("Password valid");
}
Browser Compatibility
trimEnd() is supported in all modern browsers and Node.js 12+. For older environments, use the legacy trimRight() or a polyfill:
if (!StringtrimEnd) {
StringtrimEnd = function() {
return this.replace(/\s+$/, '');
};
}
Key Behaviors
- Returns a new string; original is unchanged
- Removes whitespace only from the end (right side)
- Returns the original string if no trailing whitespace exists
- Identical to the legacy
trimRight()method
See Also
- String.trim() — Remove whitespace from both ends
- String.trimStart() — Remove whitespace from the start