Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.trimEnd()

BaselineWidely available

ThetrimEnd() method ofString values removes whitespace from the end of this string and returns a new string, without modifying the original string.trimRight() is an alias of this method.

Try it

const greeting = "   Hello world!   ";console.log(greeting);// Expected output: "   Hello world!   ";console.log(greeting.trimEnd());// Expected output: "   Hello world!";

Syntax

js
trimEnd()trimRight()

Parameters

None.

Return value

A new string representingstr stripped of whitespace from its end (right side). Whitespace is defined aswhite space characters plusline terminators.

If the end ofstr has no whitespace, a new string is still returned (essentially a copy ofstr).

Aliasing

Aftertrim() was standardized, engines also implemented the non-standard methodtrimRight. However, for consistency withpadEnd(), when the method got standardized, its name was chosen astrimEnd. For web compatibility reasons,trimRight remains as an alias totrimEnd, and they refer to the exact same function object. In some engines this means:

js
String.prototype.trimRight.name === "trimEnd";

Examples

Using trimEnd()

The following example trims whitespace from the end ofstr, but not from its start.

js
let str = "   foo  ";console.log(str.length); // 8str = str.trimEnd();console.log(str.length); // 6console.log(str); // '   foo'

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.trimend

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp