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

Specification Status Comment
String.prototype.{trimStart,trimEnd} proposal Stage 1

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 66
66
Yes
Uses the non-standard name: trimRight
? 61
61
3.5
Uses the non-standard name: trimRight
No 53 ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 66
66
Yes
Uses the non-standard name: trimRight
66
66
Yes
Uses the non-standard name: trimRight
? 61
61
4
Uses the non-standard name: trimRight
53 ? ?
Server
Node.js
Basic support 10.0.0

See also

© 2005–2018 Mozilla Developer Network and individual 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