String.prototype.trim() returns a new string with whitespace removed from both ends. Spaces between words stay. It takes no parameters and never mutates the original.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.trim. Start with this line.
JavaScript
string.trim()
Call
What it does
string.trim()
Primary signature.
string.trim(...)
Call with the arguments your case needs.
Built into JavaScript. Call trim(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
string.trim()
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
New string without leading/trailing whitespace
JavaScript Examples
Example 1 — Trim Both Ends
Remove leading and trailing spaces from a simple string.
Both ends.trim() removes leading and trailing spaces.
Start only.trimStart() leaves the trailing spaces.
End only.trimEnd() leaves the leading spaces.
Rule of thumb. Default to trim() for form fields; use one-sided methods when alignment matters.
Compatibility
Browser & Runtime Support
String.prototype.trim() is Baseline Widely available — part of ES5 and supported in every modern browser and runtime for many years.
✓ Baseline · ES5+
String.prototype.trim()
Supported in all evergreen browsers, Internet Explorer 9+, and every current Node.js / Deno / Bun release.
100%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 9+ · Legacy environments
Full support
OperaAll 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
String.trim()Universal
Bottom line: Safe everywhere modern apps run. Prefer trim() for both ends; use trimStart() / trimEnd() when you need one-sided cleanup.
Frequently Asked Questions
trim() returns a new string with whitespace removed from both the beginning and the end. The original string is not changed.
Whitespace includes spaces, tabs, newlines, and other white space characters plus line terminators (as defined by the ECMAScript whitespace and line terminator rules).
No. Only leading and trailing whitespace are removed. Spaces between words stay. Use replace with a regex if you need to collapse inner spaces.
trim() cleans both ends. trimStart() (alias trimLeft) removes only from the start. trimEnd() (alias trimRight) removes only from the end.
No. Strings are immutable. trim() always returns a new string — even when there was nothing to remove.
Almost always when reading user input, query strings, or pasted text — trim before comparing, validating, or storing values.