JavaScript String trim() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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()
CallWhat 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 ValueDescription
*New string without leading/trailing whitespace

JavaScript Examples

Example 1 — Trim Both Ends

Remove leading and trailing spaces from a simple string.

JavaScript
const text = "  Hello  ";

console.log(JSON.stringify(text.trim()));  // "Hello"
console.log(text);                         // "  Hello  " (unchanged)
Try It Yourself

How It Works

  1. Start with padded text. Spaces sit before and after Hello.
  2. Call trim(). Both ends are cleaned in one step → "Hello".
  3. Original stays. text is still " Hello ".
  4. JSON.stringify helps. Quotes make the cleaned result obvious in the console.

Example 2 — Middle Spaces & Other Whitespace

Tabs and newlines at the edges go away; inner spaces remain.

JavaScript
console.log(JSON.stringify("\tHi\n".trim()));  // "Hi"
console.log(JSON.stringify("a b".trim()));     // "a b"
console.log(JSON.stringify("   ".trim()));     // ""
Try It Yourself

How It Works

  1. Tabs and newlines count. Edge \t and \n are whitespace → removed.
  2. Middle stays. The space in "a b" is not at an end → kept.
  3. All whitespace. A string of only spaces becomes "".
  4. Common use. Trim before treating empty input as “no value.”

Example 3 — trim() vs trimStart() / trimEnd()

Choose both ends, start only, or end only.

JavaScript
const text = "  Hello  ";

console.log(JSON.stringify(text.trim()));       // "Hello"
console.log(JSON.stringify(text.trimStart()));  // "Hello  "
console.log(JSON.stringify(text.trimEnd()));    // "  Hello"
Try It Yourself

How It Works

  1. Both ends. trim() removes leading and trailing spaces.
  2. Start only. trimStart() leaves the trailing spaces.
  3. End only. trimEnd() leaves the leading spaces.
  4. Rule of thumb. Default to trim() for form fields; use one-sided methods when alignment matters.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 9+ · Legacy environments
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

Next: trimStart()

Learn how to remove whitespace from the beginning of a string only.

trimStart() tutorial →

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.

15 people found this page helpful