Tip: Distinguish Between null and undefined Early
In JavaScript, accessing a missing property often returns undefined, while intentional absence is null. Mixing them up causes unexpected TypeErrors.
How to avoid it:
- Check for both explicitly when the distinction matters:
if (value === null || value === undefined) - Use the nullish coalescing operator (
??) for default values:const result = value ?? defaultValue - Avoid
if (!value)when you only mean "check for null/undefined", as it also catches0,false, and''.
Quick check: Run console.log(value === undefined) alongside console.log(value == undefined) to see the difference in your codebase.
