String.prototype.trimEnd() returns a new string with whitespace removed from the end only (same API as MDN String.prototype.trimEnd()). Learn the trimRight alias, how it differs from trim() / trimStart(), five examples, and try-it labs.
01
Kind
Instance method
02
Returns
New string
03
Parameters
None
04
Removes
End only
05
Alias
trimRight()
06
Baseline
Widely available
Fundamentals
Introduction
Sometimes you need to drop trailing spaces or newlines but keep leading indentation. That is exactly what trimEnd() does — clean the right side only.
After trim() was standardized, engines also had a non-standard trimRight(). The standard name is trimEnd() (to match padEnd()), and trimRight remains as an alias of the same function for compatibility.
💡
Beginner tip
Prefer trimEnd() in new code. Use plain trim() when both ends should be cleaned (typical form fields).
str.trimEnd() builds a new string with trailing whitespace removed. Leading whitespace and middle spaces stay unchanged.
Returns a new string — the original is unchanged.
Takes no parameters.
Only the end (right side) is cleaned.
trimRight is the same function (alias).
Foundation
📝 Syntax
General form of String.prototype.trimEnd:
JavaScript
str.trimEnd()
str.trimRight() // alias — same function
Parameters
None.
Return value
A new string representing str stripped of whitespace from its end. If there is no trailing whitespace, a new string is still returned (essentially a copy).
Exceptions
None for normal string values.
Aliasing
trimRight and trimEnd refer to the exact same function object. In some engines, String.prototype.trimRight.name === "trimEnd".
Not for form fields — usually prefer trim() (both ends).
Not mid-string spaces — use replace / regex for those.
🧠 How trimEnd() Works
1
Scan from the end
Walk backward while characters are whitespace.
End
2
Stop at first non-space
Everything before that point is kept, including leading spaces.
Keep
3
Drop the trailing run
Spaces, tabs, and line breaks at the end are removed.
Trim
4
✅
Return a new string
Original string is never modified.
Important
📝 Notes
Always returns a new string — assign it if you need the result.
Leading whitespace is intentionally kept.
trimRight is an alias — prefer trimEnd in new code.
Does not change spaces in the middle of the string.
Named to mirror padEnd() after standardization.
Compatibility
Browser & Runtime Support
String.prototype.trimEnd() is Baseline Widely available — supported across modern browsers since January 2020.
✓ Baseline · Widely available
String.trimEnd()
Safe for production in browsers and runtimes (Node.js, Deno, Bun). Prefer trimEnd() over the trimRight() alias in new code.
UniversalWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
trimEnd()Excellent
Bottom line: Use String.trimEnd() to remove trailing whitespace while keeping leading spaces. Use trim() for both ends and trimStart() for the start only.
Wrap Up
Conclusion
String.prototype.trimEnd() cleans the right side of a string — perfect when indentation must stay. Prefer it over trimRight(), and use trim() when both ends should go.
End only — leading spaces kept, trimRight is an alias.
5
Core concepts
📝01
Returns
new string
API
📏02
Removes
end only
Role
🔄03
Alias
trimRight
Compat
🔍04
Keeps
leading spaces
Rule
⚡05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.trimEnd() returns a new string with whitespace removed from the end (right side) only. Leading whitespace is kept. The original string is not changed.
Yes. trimRight() is an alias of trimEnd() — they are the same function. Prefer trimEnd() for consistency with padEnd().
Whitespace includes spaces, tabs, newlines, and other white space characters plus line terminators (same rules as trim()).
trim() cleans both ends. trimStart() removes only from the start. trimEnd() removes only from the end.
No. Strings are immutable. trimEnd() always returns a new string — even when there was nothing to remove.
When you must keep leading indentation or padding but want to drop trailing spaces or newlines — for example cleaning lines while preserving indent.
Did you know?
The name trimEnd was chosen to match padEnd. trimRight stayed as an alias so older pages and libraries keep working — in many engines even trimRight.name reports "trimEnd".