Lodash _.trimEnd() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
String utilities

What You’ll Learn

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.

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.”

📝 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!"

⚡ Quick Reference

TaskCode patternResult
Whitespace_.trimEnd(str)Trailing spaces gone
Custom_.trimEnd(str, ".")Strip trailing dots
Alias_.trimRight(str)Same function
Map rowsrows.map(_.trimEnd)Batch cleanup
Both ends_.trim(str)When start matters too
Nativestr.trimEnd()Built-in equivalent
Mutates?
No

Returns new string

Side
End only

Trailing chars

Alias
trimRight

Same API

Native
trimEnd()

ES2019+

🧰 Parameters

string Required

String to process.

_.trimEnd(line)
chars Optional

Character set for trailing removal. Default is whitespace.

_.trimEnd(id, "0")
return value New string

Copy without trailing matched characters.

const clean = _.trimEnd(raw)
leading kept Preserved

Start of string is never modified by trimEnd.

// leading indent stays

Examples Gallery

Practical _.trimEnd() patterns with copy-ready code and interactive Try It Yourself labs.

📚 Getting Started

Remove trailing spaces while keeping leading indentation.

Example 1 — Basic trailing whitespace trim

Clean the end of a padded greeting string.

javascript
const line = "   Hello, World!    ";
const trimmed = _.trimEnd(line);

console.log(JSON.stringify(trimmed));
// -> "   Hello, World!"
Try It Yourself

How It Works

Only trailing spaces are removed; the three leading spaces remain.

📈 Practical Patterns

Form fields, database cleanup, and log processing.

Example 2 — Sanitize form field trailing spaces

Users often add accidental spaces at the end of input.

javascript
const email = "user@example.com   ";
const safe = _.trimEnd(email);

console.log(safe);
// -> "user@example.com"
Try It Yourself

How It Works

trimEnd is enough when leading content must stay exactly as typed.

Example 3 — Normalize database text rows

Map trailing whitespace out of string columns.

javascript
const rows = ["alpha  ", "beta	", "gamma"];
const clean = rows.map(_.trimEnd);
// -> ["alpha", "beta", "gamma"]
Try It Yourself

How It Works

Batch trimEnd when imported data has inconsistent trailing whitespace.

Example 4 — Strip trailing dots

Remove ellipsis padding from abbreviated labels.

javascript
const label = "Loading...";
const plain = _.trimEnd(label, ".");
// -> "Loading"

How It Works

The chars argument defines which trailing characters to strip.

Example 5 — Clean log line endings

Remove trailing whitespace from a log entry without touching its prefix.

javascript
const entry = "[INFO] Task complete    ";
const clean = _.trimEnd(entry);
// -> "[INFO] Task complete"

How It Works

Preserve the level prefix while normalizing the line ending.

🚀 Beyond the Basics

Compare with trim, trimStart, and native APIs.

Example 6 — Native trimEnd() alternative

Modern JavaScript includes String.prototype.trimEnd().

javascript
const line = "  hi  ";
const native = line.trimEnd();
const lodash = _.trimEnd(line);
// both -> "  hi"

How It Works

Native trimEnd() matches default whitespace behavior. Lodash adds optional custom character trimming.

📋 Related string operations

Topic_.trimEnd_.trimStart_.trimtrimEnd()
SideEnd onlyStart onlyBoth endsEnd only
AliastrimRighttrimLefttrimRight (legacy)
Custom charsYesYesYesWhitespace only
Best forTrailing cleanupLeading cleanupFull edge cleanupNative projects

🧠 How _.trimEnd() Works

1

Receive string

Lodash accepts the input (coerced to string).

Input
2

Scan from end

Characters matching the trim set are skipped from the right.

Scan
3

Slice result

Substring from start through last kept character.

Extract
=

Return copy

New string returned; leading content preserved.

Done

📝 Notes

  • _.trimEnd() affects only the end of the string.
  • _.trimRight is an alias for the same function.
  • Pass chars to trim specific trailing characters.
  • Use _.trimStart() for the opposite side.
  • Use _.trim() when both ends need cleaning.
  • Native trimEnd() is available in ES2019+.

Conclusion

_.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.

💡 Best Practices

✅ Do

  • Assign the return value—strings are immutable
  • Combine with related helpers when building normalization pipelines
  • Validate user input types before transforming
  • Test Unicode and locale-sensitive strings when relevant
  • Use native string.trimEnd() when Lodash is not already imported

❌ Don’t

  • Expect the original string variable to change in place
  • Assume behavior matches locale-aware toLocaleLowerCase without testing
  • Trim or change casing before checking for empty input when order matters
  • Import all of Lodash for a single call if tree-shaking a tiny bundle
  • Forget to handle null/undefined coercion edge cases

Key Takeaways

01

End only

Trailing chars removed.

Basics
02

Leading kept

Start preserved.

Behavior
03

trimRight

Alias name.

API
04

Batch

map(_.trimEnd).

Pattern
05

trimStart

Opposite side.

Related

❓ Frequently Asked Questions

_.trimEnd() removes trailing characters (whitespace by default) from the end of a string and returns the result.
No. Leading characters—including leading whitespace—are preserved.
Yes. Pass a second argument: _.trimEnd(str, '.') removes trailing dots.
Lodash also exports _.trimRight as an alias for the same function.
_.trim() cleans both ends. _.trimEnd() only cleans the end, keeping intentional leading spacing.
Yes. String.prototype.trimEnd() (and trimRight in older drafts) removes trailing whitespace in modern JavaScript.
Did you know?

Lodash exports _.trimRight as an alias for _.trimEnd. For leading cleanup see _.trimStart(); for both ends use _.trim().

Practice _.trimEnd() in the Live Editor

Open Try It, run the examples, and experiment with your own strings.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful