By the end of this tutorial, you’ll use Lodash’s _.trimEnd() confidently in real string workflows.
01
Core Syntax
Call _.trimEnd(string, [chars]).
02
Trailing Only
Removes characters from the end, not the start.
03
Custom Chars
Optional charset instead of whitespace.
04
Preserve Leading
Keep intentional indentation or padding at the start.
05
Alias
_.trimRight is identical.
06
Production Tips
Normalize log lines and DB text fields.
Fundamentals
What Is _.trimEnd()?
_.trimEnd() strips characters from the end of a string while leaving the beginning untouched. It is ideal when leading spaces are meaningful—indented log lines, aligned columns, or prefixed markers—and you only need to clean trailing noise.
💡
Beginner tip
Think of _.trimEnd(line) as “remove stray spaces at the end of this line, but keep how it starts.”
Foundation
📝 Syntax
javascript
_.trimEnd(string, [chars])
Syntax Rules
string — The input string.
chars — Optional characters to remove from the end (default: whitespace).
Return value — New string with trailing matches removed.
Alias — _.trimRight is the same function.
javascript
import trimEnd from "lodash/trimEnd";
const line = " Hello, World! ";
const result = trimEnd(line);
// -> " Hello, World!"
Cheat Sheet
⚡ Quick Reference
Task
Code pattern
Result
Whitespace
_.trimEnd(str)
Trailing spaces gone
Custom
_.trimEnd(str, ".")
Strip trailing dots
Alias
_.trimRight(str)
Same function
Map rows
rows.map(_.trimEnd)
Batch cleanup
Both ends
_.trim(str)
When start matters too
Native
str.trimEnd()
Built-in equivalent
Mutates?
No
Returns new string
Side
End only
Trailing chars
Alias
trimRight
Same API
Native
trimEnd()
ES2019+
Reference
🧰 Parameters
stringRequired
String to process.
_.trimEnd(line)
charsOptional
Character set for trailing removal. Default is whitespace.
_.trimEnd(id, "0")
return valueNew string
Copy without trailing matched characters.
const clean = _.trimEnd(raw)
leading keptPreserved
Start of string is never modified by trimEnd.
// leading indent stays
Hands-On
Examples Gallery
Practical _.trimEnd() patterns with copy-ready code and interactive Try It Yourself labs.
📚 Getting Started
Remove trailing spaces while keeping leading indentation.
_.trimEnd() targets trailing whitespace and custom characters without disturbing the start of your string. Reach for it when only the end needs cleanup—form fields, log lines, and database imports are common cases.