GO-V.ORG
As seen in

String.prototype.trimEnd()

The trimEnd() method of String 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.

The **`trimEnd()`** method of String 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.

JavaScript Demo: String.prototype.trimEnd()

```js interactive-example
const greeting = " Hello world! ";

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

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

## Syntax

```js-nolint
trimEnd()

trimRight()
```

### Parameters

None.

### Return value

A new string representing `str` stripped of whitespace from its end (right side). Whitespace is defined as [white space](/docs/Web/JavaScript/Reference/Lexical_grammar#white_space) characters plus [line terminators](/docs/Web/JavaScript/Reference/Lexical_grammar#line_terminators).

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

### Aliasing

After trim() was standardized, engines also implemented the non-standard method `trimRight`. However, for consistency with padEnd(), when the method got standardized, its name was chosen as `trimEnd`. For web compatibility reasons, `trimRight` remains as an alias to `trimEnd`, 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 of `str`, but not from its start.

```js
let str = " foo ";

console.log(str.length); // 8

str = str.trimEnd();
console.log(str.length); // 6
console.log(str); // ' foo'
```

## Specifications

## Browser compatibility

## See also

- [Polyfill of `String.prototype.trimEnd` in `core-js`](https://core-js.io/docs/features/ecmascript/string-regexp)
- [es-shims polyfill of `String.prototype.trimEnd`](https://www.npmjs.com/package/string.prototype.trimend)
- String.prototype.trim()
- String.prototype.trimStart()

source: MDN Web Docs mirror · page

Cite this

"String.prototype.trimEnd()". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·