jQuery.trim(str) (alias $.trim) removes whitespace from the start and end of a string (added in jQuery 1.0). Spaces, tabs, newlines, and non-breaking spaces in the middle are kept.
Remember
$.trim(str) → trimmed string (new string)
$.trim(" hello, how are you? ") → "hello, how are you?"
// Modern equivalent:
" hello, how are you? ".trim()
Pass a string, get a cleaned copy back, then validate or save it.
Start with a string that may have edge spaces (form value, paste, API text).
Call $.trim(str) — or str.trim() in new code.
Use the returned string (the original is unchanged).
Optionally check for blank input with trimmed === "".
Piece
Required?
Role
str
Yes
String to trim
Return
—
New string without leading/trailing whitespace
Middle spaces
—
Preserved
Pseudocode
Pseudocode
function trim(str):
remove whitespace from start of str
remove whitespace from end of str
return new string // middle unchanged
// Typical flow:
raw = $("#name").val()
name = $.trim(raw) // or raw.trim()
if name === "":
show "required"
else:
save name
Reference
Cheat Sheet
Keep these facts handy while cleaning strings with jQuery.
Same input. One padded string is trimmed two ways.
Legacy.$.trim still works on jQuery 3.x.
Native.raw.trim() is the supported replacement.
Migrate. Replace $.trim(str) with String(str).trim() when the value might not be a string.
Edge Cases & Pitfalls
Check these before cleaning real form or API text.
deprecated
Removed in jQuery 4.0
Do not start new features on $.trim. Use str.trim() so upgrades stay smooth.
types
Native trim is strings-only
String.prototype.trim does not accept null, undefined, or numbers. Migrate with String(value).trim() when input type varies.
middle
Does not collapse inner spaces
trim never squeezes "a b" into "a b". Use a replace if you need that.
blank
Whitespace-only becomes ""
After trim, " " and "\n\t" are empty strings — handy for required-field checks.
Compatibility
Browser Support
jQuery.trim() existed in jQuery 1.0–3.x, was deprecated in 3.5, and removed in 4.0. Prefer native String.prototype.trim() in all modern browsers. This page focuses on browser use with jQuery loaded.
✓ Deprecated · jQuery 3.5
jQuery.trim()
Available with jQuery 1.0–3.x in Chrome, Firefox, Safari, and Edge. Prefer String.prototype.trim() for new code. Not present in jQuery 4.0.
LegacyPrefer .trim()
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 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
$.trim()Legacy
Bottom line: Learn $.trim() for legacy jQuery code. Write str.trim() (or String(value).trim()) in new projects.
Analysis
Performance Notes
Approach
Cost
Best for
str.trim()
O(n) in string length
All new code
$.trim(str)
O(n)
Legacy jQuery 1.x–3.x only
Trim once per field
Cheapest habit
Form submit / blur handlers
Trimming is cheap for normal form strings. Prefer one trim when you read the value, then reuse that cleaned string for validation and save.
Remember
Key Takeaways
Role:$.trim(str) removes edge whitespace only.
Modern: use str.trim() instead.
Middle: inner spaces stay; trim does not collapse them.
Status: deprecated in 3.5, removed in 4.0.
One line: know $.trim for legacy jQuery, but clean strings with str.trim() (and String(value).trim() when needed) in every new project.
Frequently Asked Questions
jQuery.trim(str) removes whitespace — spaces, tabs, newlines, and non-breaking spaces — from the beginning and end of a string. Whitespace in the middle stays unchanged. It returns a new string and does not modify the original.
The jQuery API demo is $.trim(" hello, how are you? "), which returns "hello, how are you?" — outer spaces gone, inner spacing preserved.
Both strip leading and trailing whitespace. Native String.prototype.trim() is the modern standard. $.trim was deprecated in jQuery 3.5 and removed in 4.0. Native trim only works on strings — convert with String(value).trim() when migrating non-string input.
No. Only leading and trailing whitespace is removed. $.trim(" hello world ") returns "hello world".
Not for new code. Prefer " text ".trim() or String(value).trim(). Keep $.trim only while maintaining jQuery 1.x–3.x legacy code.
Common uses included cleaning form values before validation, normalizing pasted textarea text, and preparing strings for comparison in older jQuery plugins.