W3cubDocs

/JavaScript

string.trimEnd

The trimEnd() method removes whitespace from the end of a string. trimRight() is an alias of this method.

Syntax

str.trimEnd();
str.trimRight();

Return value

A new string representing the calling string stripped of whitespace from its (right) end.

Description

The trimEnd() / trimRight() methods return the string stripped of whitespace from its right end. trimEnd() or trimRight() do not affect the value of the string itself.

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 compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
trimEnd 66
66
4
Uses the non-standard name: trimRight
12
12
Uses the non-standard name: trimRight
61
61
3.5
Uses the non-standard name: trimRight
No 53
53
15
Uses the non-standard name: trimRight
12
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
trimEnd 66
66
≤37
Uses the non-standard name: trimRight
66
66
18
Uses the non-standard name: trimRight
61
61
4
Uses the non-standard name: trimRight
47
47
14
Uses the non-standard name: trimRight
12 9.0
9.0
1.0
Uses the non-standard name: trimRight
Server
Node.js
trimEnd 10.0.0
10.0.0
0.12
Uses the non-standard name: trimRight

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd