JavaScript
Remove null and undefined without dropping zero
Use an explicit nullish predicate when filtering values. Boolean filtering also removes zero, false, and empty strings, which may be legitimate data rather than missing entries.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const input = [0, false, "", null, undefined, 7];
const kept = input.filter(value => value !== null && value !== undefined);
console.log(JSON.stringify(kept));How it works
Filter creates an array containing only the entries for which the callback returns true. Comparing against both null and undefined preserves all other values. This is suitable for form fields or measurements where zero carries meaning.
Limits and mistakes
This does not remove NaN, whitespace-only strings, empty arrays, or objects with missing properties. Those require separate validation rules. Do not broaden the predicate until you know which values your application considers absent.
Check the result
Expect [0,false,"",7]. Try a row containing only zero and false; it should not become empty. Keep missing-value cleanup separate from type validation.
Sources
Reference documentation for the behavior used in this guide: