String.prototype.trim() returns a new string with whitespace removed from both ends (same API as MDN String.prototype.trim()). Learn what counts as whitespace, how it differs from trimStart() / trimEnd(), cleaning form input, five examples, and try-it labs.
01
Kind
Instance method
02
Returns
New string
03
Parameters
None
04
Removes
Both ends
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
User input and pasted text often include accidental spaces, tabs, or newlines at the edges. trim() strips that padding from both ends so comparisons and validation stay reliable.
Middle spaces stay put — "Hello world" remains two words. For only the start or only the end, use trimStart() or trimEnd().
💡
Beginner tip
Almost always trim before comparing or storing form fields: const name = input.value.trim();
str.trim() builds a new string with leading and trailing whitespace removed. Whitespace means white space characters plus line terminators (spaces, tabs, newlines, and similar).
Returns a new string — the original is unchanged.
Takes no parameters.
Does not remove spaces between words.
Still returns a new string when there was nothing to trim.
Foundation
📝 Syntax
General form of String.prototype.trim:
JavaScript
str.trim()
Parameters
None.
Return value
A new string representing str with whitespace stripped from both its beginning and end.
Exceptions
None for normal string values.
Common patterns
JavaScript
" Hello world! ".trim(); // "Hello world!"
" foo ".trim(); // "foo"
"\t\n hi \r".trim(); // "hi"
"no-spaces".trim(); // "no-spaces"
" a b ".trim(); // "a b" (middle spaces kept)
Cheat Sheet
⚡ Quick Reference
Goal
Code
Trim both ends
str.trim()
Trim start only
str.trimStart()
Trim end only
str.trimEnd()
Clean form input
input.value.trim()
Trim + lowercase
str.trim().toLowerCase()
Snapshot
🔍 At a Glance
Four facts to remember about String.trim().
Returns
string
New trimmed text
Ends
both
Start and end
Middle
kept
Inner spaces stay
Mutates
no
Immutable
Compare
📋 trim() vs trimStart() vs trimEnd()
trim()
trimStart()
trimEnd()
Removes from
Both ends
Start only
End only
Old alias
—
trimLeft()
trimRight()
" hi "
"hi"
"hi "
" hi"
Best for
Form fields / defaults
Indent-sensitive text
Trailing newlines
Hands-On
Examples Gallery
Examples follow MDN String.trim() patterns plus everyday beginner tips. Use View Output or Try It Yourself for each case.
📚 Getting Started
MDN demos for everyday trimming.
Example 1 — Basic trim()
MDN Try it demo: spaces on both sides of a greeting.
String.prototype.trim() is Baseline Widely available — supported across modern browsers since July 2015 (and long before in practice).
✓ Baseline · Widely available
String.trim()
Safe for production in browsers and runtimes (Node.js, Deno, Bun). Everyday choice for cleaning leading and trailing whitespace.
UniversalWidely available
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
trim()Excellent
Bottom line: Use String.trim() to remove whitespace from both ends. Use trimStart/trimEnd for one side, and replace/regex when you need to change spaces in the middle.
Wrap Up
Conclusion
String.prototype.trim() is the simple, reliable way to clean padding from both ends of a string — especially form input. Reach for trimStart() or trimEnd() when only one side should change.
Both ends cleaned — middle kept, original unchanged.
5
Core concepts
📝01
Returns
new string
API
📏02
Removes
both ends
Role
🔢03
Middle
spaces kept
Rule
🔍04
Siblings
Start / End
Family
⚡05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.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.
Did you know?
Even when a string has no leading or trailing whitespace, trim() still returns a new string (essentially a copy). That keeps the API predictable — you never need a special “was anything trimmed?” check for correctness.