Lodash _.trim() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Removes leading and trailing whitespace — or a custom set of characters — from both ends of a string. Middle characters are left alone. Part of Lodash String (stable since 3.0.0 — not deprecated).

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

Syntax

Call _.trim. Start with this line.

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

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

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

Parameters

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

Return Value

Return ValueDescription
*New trimmed string

Lodash Examples

Example 1 — Official: Trim Whitespace

Remove spaces from both ends.

Lodash
console.log(_.trim("  abc  "));
// 'abc'
Try It Yourself

How It Works

  1. Input. Spaces sit before and after abc.
  2. Trim. Default chars is whitespace.
  3. Result. Only abc remains.
  4. Docs. Matches the official first example.

Example 2 — Official: Custom Characters

Pass a character set to strip from both ends.

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

How It Works

  1. Set. '_-' means strip _ or -.
  2. Left. Edge -, _, - are removed.
  3. Right. Same on the other end.
  4. Docs. Matches the official second example.

Example 3 — Official: Trim an Array with _.map

Pass _.trim as the iteratee.

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

How It Works

  1. map. Runs _.trim on each array item.
  2. Each. Leading/trailing spaces are removed.
  3. Result. A new array of clean strings.
  4. Docs. Matches the official third example.

Example 4 — Middle Spaces Are Kept

Only the edges are cleaned.

Lodash
console.log(_.trim("  hello   world  "));
// 'hello   world'
Try It Yourself

How It Works

  1. Edges. Leading and trailing spaces go away.
  2. Middle. The gap between words stays.
  3. Why. _.trim is not a “collapse spaces” helper.
  4. Tip. Combine with replace if you need single spaces.

Example 5 — vs _.trimStart / _.trimEnd

Choose both ends or one side.

Lodash
var s = "  abc  ";

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

How It Works

  1. trim. Cleans both ends.
  2. trimStart. Leaves trailing spaces.
  3. trimEnd. Leaves leading spaces.
  4. Next. See _.trimEnd and _.trimStart.

Browser & Runtime Support

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

✓ Stable · Lodash 4.x

_.trim()

Available in [email protected] full builds and the lodash/trim 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
_.trim Since 3.0.0

Bottom line: Safe for production once Lodash is on the page. Prefer import trim from "lodash/trim" for smaller builds. Use native String#trim when you only need whitespace and do not need Lodash.

Frequently Asked Questions

_.trim([string=''], [chars=whitespace]) removes leading and trailing whitespace or specified characters. Official: _.trim(' abc ') → 'abc'.
No. Only characters at the start and end are removed. Inner spaces stay intact.
It is a set of characters to strip from both ends — not an exact substring. _.trim('-_-abc-_-', '_-') removes any mix of '_' and '-' from the edges.
_.trim removes from both ends. _.trimStart removes only from the start; _.trimEnd only from the end.
No. Strings are immutable. _.trim always returns a new string.
Use import trim from "lodash/trim" or require("lodash/trim"). Full build: import _ from "lodash" then _.trim.

Next: _.trimEnd()

Remove characters from the end of a string.

_.trimEnd() 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