trim()
trim(
str):string
Removes leading and trailing whitespace from a string.
DEPRECATED
Use string.trim() directly instead.
Parametersโ
str: string | null | undefinedโ
The string to trim.
Returns: stringโ
The trimmed string.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
trim (Lodash, es-toolkit, Ramda, Effect, Modern Dash) ยท โ (Remeda, Radashi, Antfu)
Exampleโ
// โ Deprecated approach
trim(' hello world '); // => 'hello world'
// โ
Recommended approach
' hello world '.trim(); // => 'hello world'
How it works?โ
Removes whitespace from both ends of a string.
Deprecated: Use string.trim() directly.
Native Equivalentโ
// โ trim(' hello ')
// โ
' hello '.trim()
Use Casesโ
Remove whitespace ๐โ
Remove leading/trailing whitespace.
" hello ".trim(); // => "hello"
" hello ".trimStart(); // => "hello "
" hello ".trimEnd(); // => " hello"
Clean user inputโ
Normalize form input.
const email = inputValue.trim().toLowerCase();
Process text linesโ
Clean each line in multiline text.
const lines = text.split("\n").map(l => l.trim());
Clean imported CSV or Excel dataโ
Strip invisible characters and extra whitespace from imported spreadsheet data. Critical when processing user-uploaded CSV/Excel files with inconsistent formatting.
const rawCells = [" John Doe ", "\tAlice\t", " Bob ", "\u00A0Charlie\u00A0"];
const cleaned = rawCells.map((cell) => trim(cell));
// => ["John Doe", "Alice", "Bob", "Charlie"]