Skip to content

isNullish

Checks if a value is null or undefined (nullish).

Available since v2.0.0

import { isNullish } from '@helpers4/guard';
isNullish(value: unknown): value is null | undefined
ParameterTypeDescription
valueunknownThe value to check

value is null | undefined — True if value is null or undefined

Returns true only for null and undefined, not other falsy values.

isNullish(null)      // => true
isNullish(undefined) // => true
isNullish(0)         // => false
isNullish('')        // => false

Use as a type guard to safely narrow types.

function greet(name: string | null | undefined): string {
  if (isNullish(name)) return 'Hello, stranger!';
  return `Hello, ${name}!`;
}
greet(null) // => 'Hello, stranger!'

View source on GitHub