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])
Call
What 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
Parameter
Description
string
Optional. Value to trim (default '')
chars
Optional. Characters to strip from both ends (default: whitespace)
_.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 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
_.trimSince 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.