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