Use loose equality (`== null`) instead of strict equality (`=== null` or `=== undefined`) when checking for both null and undefined values. In most JavaScript contexts, null and undefined should be treated equivalently, and loose equality provides a more concise and readable way to handle both cases.
Use loose equality (== null
) instead of strict equality (=== null
or === undefined
) when checking for both null and undefined values. In most JavaScript contexts, null and undefined should be treated equivalently, and loose equality provides a more concise and readable way to handle both cases.
This pattern is particularly useful for:
Example:
// Instead of this:
if (typeof process.env[address] === 'undefined') missingEnvVariables.add(address);
if (startingPosition === 'AT_TIMESTAMP' && !(startingPositionTimestamp !== undefined && startingPositionTimestamp !== null)) {
// Use this:
if (process.env[address] == null) missingEnvVariables.add(address);
if (startingPosition === 'AT_TIMESTAMP' && startingPositionTimestamp == null) {
The loose equality check (== null
) catches both null
and undefined
values in a single, readable condition, making code more maintainable and following JavaScript’s conventional approach to null safety.
Enter the URL of a public GitHub repository