Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.trimEnd

Removes whitespace and line breaks from the end of a string.

Available since: ES2019

Syntax

str.trimEnd()

Return value

stringA new string without trailing whitespace.

Examples

Leading spaces stay:

console.log(`[${'   Anna  '.trimEnd()}]`);

Output:

[   Anna]

Removing a trailing line break:

const line = 'last line\n';
 
console.log(line.trimEnd().length);

Output:

9

Notes

Line breaks, tabs and non-breaking spaces also count as whitespace.

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.