Skip to content

Function: trimToEmpty()

trimToEmpty(value): string

trimToNull with an empty-string miss instead of null, for callers that build a string unconditionally and treat “absent” as “”.

Reading loosely-typed JSON is the motivating case: a field that should be a string may be missing or the wrong type, and the caller wants "" rather than a null check at every access.

unknown

string

const title = trimToEmpty(record.title); // always a string