Trimming removes whitespace from the start and end of a value: spaces, tabs, line breaks, and non-breaking spaces. Full cleanup also removes invisible zero-width characters and collapses repeated spaces inside the value.
Why it matters when importing data
Whitespace is invisible in a spreadsheet and fatal in a database. "Acme " and "Acme" fail an equality check, an email with a trailing space fails validation, and a copied value with a zero-width character looks identical to a correct one but never matches. Trimming on import removes a whole class of errors nobody can see.
Trimming Fields in JavaScript
trim() handles ordinary whitespace, including non-breaking spaces and a byte order mark at either end, but not zero-width spaces or repeated spaces in the middle. In the output below, · marks a space and hidden characters are shown in brackets:
// trim() removes spaces, tabs, line breaks, non-breaking spaces, and a byte order
// mark at either end. Zero-width spaces (U+200B to U+200D) are not whitespace to
// trim(), so they need removing explicitly, and runs of spaces inside the value
// need collapsing.
const clean = (value) =>
String(value ?? "")
.replace(/[\u200B-\u200D\uFEFF]/g, "")
.trim()
.replace(/\s+/g, " ");
const values = [
" Acme Corp ",
"Globex\t",
"\u00A0Initech\u00A0",
"Hooli\u200B",
"Stark Industries",
];
// Make hidden characters visible so the difference shows up in the output.
const show = (value) =>
value
.replace(/ /g, "·")
.replace(/\t/g, "[TAB]")
.replace(/\u00A0/g, "[NBSP]")
.replace(/\u200B/g, "[ZWSP]");
const results = values.map((value) => ({
before: show(value),
trimOnly: show(value.trim()),
cleaned: show(clean(value)),
}));
console.table(results); Output
| Value | trim() only | Full cleanup |
|---|---|---|
| ··Acme·Corp· | Acme·Corp | Acme·Corp |
| Globex[TAB] | Globex | Globex |
| [NBSP]Initech[NBSP] | Initech | Initech |
| Hooli[ZWSP] | Hooli[ZWSP] | Hooli |
| Stark····Industries | Stark····Industries | Stark·Industries |
Edge cases to handle
- Zero-width spaces. Characters U+200B to U+200D often arrive in text copied from web pages, and a byte order mark (U+FEFF) can end up inside a value when files are joined.
trim()removes neither from the middle of a value, so strip both explicitly. - Internal spaces. Collapse runs of spaces for names and labels, but not for fields where spacing is meaningful.
- Trim before validating. Validation should see the cleaned value, or a trailing space fails a perfectly good email.
- Leading zeros are not whitespace. Trimming should never touch
00342style zip codes or account numbers.
How Dromo handles trimming fields
In Dromo, a column hook can trim and collapse whitespace for an entire column before validation runs, so validators see the cleaned value. Multi-select fields trim each value after splitting by default through the trimValues option.
Related terms
- Normalizing Cases: Converting text to a consistent case so equal values compare as equal.
- Finding Text Within a String: Locating a substring inside a value, and optionally replacing it.
- Requiring Values: Making sure required fields contain a real value.
Frequently asked questions
What does trim() do in JavaScript?
trim() returns the string with whitespace removed from both ends, including spaces, tabs, line breaks, and non-breaking spaces. It does not change whitespace in the middle and does not remove zero-width spaces (U+200B to U+200D).
Why does my value still not match after trimming?
It probably contains a zero-width space, which trim() does not remove, or a byte order mark in the middle of the value. Remove characters U+200B to U+200D and U+FEFF and compare again.