transformation

String.prototype.trimEnd

Returns a new string with trailing whitespace removed. Added in ES2019 (aliased as trimRight for legacy code).

str.trimEnd()

Parameters

Parameter Purpose
(no arguments) Method takes no parameters
returns New string with trailing whitespace stripped

Examples

console.log('   hello   '.trimEnd());

Prints ' hello'

console.log('abc\n\t '.trimEnd());

Prints 'abc'

console.log('   abc'.trimEnd());

Prints ' abc'

console.log('hi   '.trimEnd().length);

Prints 2

Gotcha

trimRight is a legacy alias — prefer trimEnd. Interior and leading whitespace are untouched.

Related methods

← All JS string methods · JS array methods