For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/string/trimEnd.md.

trimEnd es-toolkit

Removes whitespace or specified characters from the end of a string.

const trimmed = trimEnd(str, chars);

Usage

trimEnd(str, chars?)

Use trimEnd when you want to remove unnecessary characters from the end of a string. If no specific characters are specified, it removes whitespace characters.

import { trimEnd } from 'es-toolkit/string';

// Basic whitespace removal
trimEnd('hello  '); // 'hello'
trimEnd('hello\t\n  '); // 'hello'

// Removing specific characters
trimEnd('hello---', '-'); // 'hello'
trimEnd('123000', '0'); // '123'
trimEnd('abcabcabc', 'c'); // 'abcabcab'

When you specify multiple characters as an array, all characters that match any of them will be removed.

import { trimEnd } from 'es-toolkit/string';

// Specifying multiple characters as an array
trimEnd('hello!!@@', ['!', '@']); // 'hello'

// Removing numbers and special characters
trimEnd('abc123', ['1', '2', '3']); // 'abc'

// Removing characters and whitespace together
trimEnd('hello__  ', ['_', ' ']); // 'hello'

Parameters

strstringrequired

The string from which to remove characters from the end.

charsstring | string[]optional

The characters to remove. Can be a string or an array of characters. Defaults to whitespace characters.

Returns

Returns a new string with the specified characters removed from the end.

string

Errors

Throws an error if chars is a string with a length other than 1.

Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.