JavaScript String trimEnd() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.trimEnd() returns a new string with whitespace removed from the end only. Leading spaces stay. It takes no parameters. trimRight() is the same function (legacy alias).

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.trimEnd. Start with this line.

JavaScript
string.trimEnd()
CallWhat it does
string.trimEnd()Primary signature.
string.trimEnd(...)Call with the arguments your case needs.

Built into JavaScript. Call trimEnd(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
string.trimEnd()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*New string without trailing whitespace

JavaScript Examples

Example 1 — Trim the End Only

Remove trailing spaces; leave leading spaces in place.

JavaScript
const text = "  Hello  ";

console.log(JSON.stringify(text.trimEnd()));  // "  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 trimEnd(). Only the trailing spaces are removed → " Hello".
  3. Leading stays. The two spaces at the start remain.
  4. Original stays. text is still " Hello ".

Example 2 — Keep Leading Indent

Drop a trailing newline without losing indentation.

JavaScript
const line = "  code();\n";

console.log(JSON.stringify(line.trimEnd()));  // "  code();"
console.log(JSON.stringify(line.trim()));     // "code();"
Try It Yourself

How It Works

  1. Indented line. Leading spaces are meaningful indent; a trailing \n is noise.
  2. trimEnd cleans the end. Newline gone; indent kept → " code();".
  3. trim() would strip indent. Both ends cleaned → "code();".
  4. When to choose. Prefer trimEnd when leading layout must survive.

Example 3 — vs trim() / trimRight() Alias

Compare both-end cleanup with the end-only methods (including the alias).

JavaScript
const text = "  Hello  ";

console.log(JSON.stringify(text.trim()));       // "Hello"
console.log(JSON.stringify(text.trimEnd()));    // "  Hello"
console.log(JSON.stringify(text.trimRight()));  // "  Hello"
console.log(String.prototype.trimEnd === String.prototype.trimRight); // true
Try It Yourself

How It Works

  1. trim() clears both sides. Result is "Hello".
  2. trimEnd() clears the end. Leading spaces remain → " Hello".
  3. trimRight() matches. It is an alias — same result as trimEnd().
  4. Same function. The === check on the prototype methods is true.

Browser & Runtime Support

String.prototype.trimEnd() is part of ES2019 and is Baseline Widely available in modern browsers and runtimes. The trimRight alias is also widely supported.

✓ Baseline · ES2019

String.prototype.trimEnd()

Supported in Chrome 66+, Firefox 61+, Safari 12+, Edge 79+, and modern Node.js. Prefer trimEnd() over trimRight() in new code.

95% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · 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.trimEnd() Excellent

Bottom line: Safe for modern web apps and current Node LTS. For very old engines, feature-detect or fall back to a small regex / polyfill — or use trim() when both ends are fine to clean.

Frequently Asked Questions

trimEnd() returns a new string with whitespace removed from the end (right side) only. Leading whitespace is kept. The original string is not changed.
Yes. trimRight() is an alias of trimEnd() — they are the same function. Prefer trimEnd() for consistency with padEnd().
Whitespace includes spaces, tabs, newlines, and other white space characters plus line terminators (same rules as trim()).
trim() cleans both ends. trimStart() removes only from the start. trimEnd() removes only from the end.
No. Strings are immutable. trimEnd() always returns a new string — even when there was nothing to remove.
When you must keep leading indentation or padding but want to drop trailing spaces or newlines — for example cleaning lines while preserving indent.

Next: replace()

Learn how to replace a substring or pattern with new text.

replace() 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.

10 people found this page helpful