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"]