jQuery trim() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.trim(str)  → trimmed string (new string)

$.trim(" hello, how are you? ")  → "hello, how are you?"

// Modern equivalent:
" hello, how are you? ".trim()
CallWhat it does
trim()Learn jQuery.trim(): remove leading and trailing whitespace, keep middle spaces, official demo, migrate to String.prototype.trim, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with a string that may have edge spaces (form value, paste, API text).
Step 2Call $.trim(str) — or str.trim() in new code.
Step 3Use the returned string (the original is unchanged).

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official Demo

Official jQuery API pattern in one line.

jQuery
var result = $.trim(" hello, how are you? ");

console.log(result);  // "hello, how are you?"
Try It Yourself

How It Works

  1. Input. The string has one space at each end.
  2. Trim. $.trim removes those edge characters.
  3. Middle. The space after the comma stays — only edges change.

Example 2 — Middle Spaces Are Kept

Extra spaces between words are not collapsed.

jQuery
var text = "  hello   world  ";
var cleaned = $.trim(text);

console.log(JSON.stringify(cleaned));  // "hello   world"
Try It Yourself

How It Works

  1. Edges. Leading and trailing double spaces are removed.
  2. Middle. The three spaces between hello and world remain.
  3. JSON.stringify. Quotes make the remaining spaces easy to see in output.

Example 3 — Newlines and Tabs

Pasted text often includes line breaks and tabs at the edges.

jQuery
var pasted = "\n\t  Save draft  \r\n";
var cleaned = $.trim(pasted);

console.log(JSON.stringify(cleaned));  // "Save draft"
Try It Yourself

How It Works

  1. Whitespace kinds. \n, \t, \r, and spaces all count at the edges.
  2. Trim. Every leading/trailing whitespace character is stripped.
  3. Result. Only the visible words remain: Save draft.

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.

Next: jQuery.now() Method

Learn how jQuery returns the current timestamp in milliseconds.

jQuery.now() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful