The trimEnd()
method removes whitespace from the end of a string. trimRight()
is an alias of this method.
String.prototype.trimEnd()
Try it
Syntax
trimEnd() trimRight()
Return value
A new string representing str
stripped of whitespace from its end (right side).
If the end of str
has no whitespace, a new string is still returned (essentially a copy of str
), with no exception being thrown.
Aliasing
For consistency with functions like String.prototype.padEnd
the standard method name is trimEnd
. However, for web compatibility reasons, trimRight
remains as an alias to trimEnd
. In some engines this means:
String.prototype.trimRight.name === "trimEnd";
Examples
Using trimEnd()
The following example displays the lowercase string ' foo'
:
var str = ' foo '; console.log(str.length); // 8 str = str.trimEnd(); console.log(str.length); // 6 console.log(str); // ' foo'
Specifications
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
trimEnd |
66
4
|
79
12
|
61
3.5
|
No
|
53
15
|
12
|
66
≤37
|
66
18
|
61
4
|
47
14
|
12
|
9.0
1.0
|
1.0
1.0
|
10.0.0
0.12.0
|
See also
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd