Write code assuming inputs (arrays, objects, DOM nodes) may contain undefined, null, or “empty slots/holes”, and normalize them early with explicit defaults.
Standards
zone), assign a deterministic default ("general").undefined values and array holes (e.g., [, ]), not just one.Example
function groupByZone(actions) {
return actions.reduce((acc, action) => {
const zone = action?.item?.zone ?? 'general'; // default when missing/undefined
(acc[zone] ??= []).push(action);
return acc;
}, {});
}
// Null-safe DOM access
const el = document.querySelector('.container > div');
console.assert(el?.classList.contains('output'));
console.assert(el?.classList.contains('hide'));
// Example for array-hole aware compaction
function compactFragments(arr) {
return arr.filter(Boolean); // removes undefined/null and also skips holes
}
Enter the URL of a public GitHub repository