Lodash _.trimEnd() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Removes trailing whitespace — or a custom set of characters — from the end of a string. Leading characters stay. Part of Lodash String (stable since 4.0.0 — not deprecated; _.trimRight is an alias).

Pass the string and options shown below. Check the return type before you chain another helper.

Syntax

Call _.trimEnd. Start with this line.

Lodash
_.trimEnd([string=''], [chars=whitespace])
CallWhat it does
_.trimEnd([string=''], [chars=whitespace])Primary signature.
_.trimEnd(string)Transform or inspect a string.

Prefer import trimEnd from "lodash/trimEnd" when you want a smaller bundle.

Library declarations
Lodash
_.trimEnd([string=''], [chars=whitespace])

Parameters

ParameterDescription
stringOptional. Value to trim (default '')
charsOptional. Characters to strip from the end (default: whitespace)

Return Value

Return ValueDescription
*New string with trailing chars removed

Lodash Examples

Example 1 — Official: Trim Trailing Whitespace

Spaces on the right go away; spaces on the left stay.

Lodash
console.log(JSON.stringify(_.trimEnd("  abc  ")));
// '  abc'
Try It Yourself

How It Works

  1. Input. Spaces sit on both sides of abc.
  2. End. Trailing spaces are removed.
  3. Start. Leading spaces remain.
  4. Docs. Matches the official first example.

Example 2 — Official: Custom Characters

Strip a character set from the end only.

Lodash
console.log(_.trimEnd("-_-abc-_-", "_-"));
// '-_-abc'
Try It Yourself

How It Works

  1. Set. '_-' means strip _ or -.
  2. Right. Trailing -, _, - are removed.
  3. Left. The leading -_- stays.
  4. Docs. Matches the official second example.

Example 3 — Leading Indent Is Kept

Useful when indentation on the left should remain.

Lodash
var line = "    code;  ";

console.log(JSON.stringify(_.trimEnd(line)));
// '    code;'
Try It Yourself

How It Works

  1. Indent. Leading spaces look like code indent.
  2. trimEnd. Only trailing spaces disappear.
  3. Result. Indent + content remain.
  4. Use. Cleaning line ends without flattening indentation.

Example 4 — Clean Trailing Spaces in a List

Map _.trimEnd over several values.

Lodash
console.log(
  _.map(["foo  ", "  bar  "], _.trimEnd)
);
// ['foo', '  bar']
Try It Yourself

How It Works

  1. map. Runs _.trimEnd on each item.
  2. First. 'foo ' → 'foo'.
  3. Second. Leading spaces on bar stay.
  4. Tip. Use _.trim if both ends should go.

Example 5 — vs _.trim / _.trimStart

Pick the helper that matches the side you want cleaned.

Lodash
var s = "  abc  ";

console.log(JSON.stringify(_.trimEnd(s)));   // '  abc'
console.log(JSON.stringify(_.trimStart(s))); // 'abc  '
console.log(JSON.stringify(_.trim(s)));      // 'abc'
Try It Yourself

How It Works

  1. trimEnd. Cleans the right side only.
  2. trimStart. Cleans the left side only.
  3. trim. Cleans both sides.
  4. Next. See _.trimStart.

Browser & Runtime Support

_.trimEnd is a Lodash String helper available since 4.0.0 (_.trimRight alias). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x

_.trimEnd()

Available in [email protected] full builds and the lodash/trimEnd modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.trimEnd Since 4.0.0

Bottom line: Safe for production once Lodash is on the page. Prefer import trimEnd from "lodash/trimEnd" for smaller builds. Native String#trimEnd is fine for whitespace-only cases without Lodash.

Frequently Asked Questions

_.trimEnd([string=''], [chars=whitespace]) removes trailing whitespace or specified characters from the end only. Official: _.trimEnd(' abc ') → ' abc'.
No. Leading characters — including leading whitespace — are preserved.
It is a set of characters to strip from the end — not an exact substring. _.trimEnd('-_-abc-_-', '_-') removes any mix of '_' and '-' from the right.
_.trim cleans both ends. _.trimStart cleans only the start. _.trimEnd cleans only the end.
Yes. _.trimRight is an alias of _.trimEnd in Lodash 4.x. Prefer _.trimEnd in new code.
Use import trimEnd from "lodash/trimEnd" or require("lodash/trimEnd"). Full build: import _ from "lodash" then _.trimEnd.

Next: _.trimStart()

Remove characters from the start of a string.

_.trimStart() tutorial →

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.

10 people found this page helpful