<!--
title: Null-Safe Optional Handling
domain: app-frameworks
topic: Null Handling
language: JavaScript
source: tanstack/query
updated: 2020-03-27
url: https://awesomereviewers.com/reviewers/query-null-safe-optional-handling/
-->

When dealing with possibly missing or nullable values (especially globals and config), make null/undefined handling explicit and never call a value unless you’ve verified its type.

Apply this standard:
- Use explicit checks for undefined: `x === undefined` (or `x !== undefined`) rather than relying on implicit truthiness.
- Guard optional callbacks: before calling `maybeFn(...)`, ensure `typeof maybeFn === 'function'`.
- If refactoring boolean logic around undefined/missing values, add/adjust unit tests to confirm behavior is unchanged.

Example:
```js
function isDocumentVisible(document) {
  return (
    document.visibilityState === undefined ||
    document.visibilityState === 'visible' ||
    document.visibilityState === 'prerender'
  );
}

function shouldRetry(query, error) {
  const retry = query.config.retry;

  if (retry === true) return true;
  if (typeof retry === 'number') return query.state.failureCount <= retry;
  if (typeof retry === 'function') {
    return retry(query.state.failureCount, error);
  }
  return false;
}
```
