Skip to content

isNullish function ​

Checks whether a value is null or undefined: the opposite of isDefined.

ts
import { isNullish } from 'utils';

Signature ​

ts
export function isNullish(value: unknown): value is null | undefined

Parameters ​

NameDescription
valueThe value to check.

Returns ​

true for null and undefined; false for 0, '' and false.

Example ​

ts
isNullish(undefined); // true
isNullish(0); // false

Tests ​

Line coverage: 100 %.

  • ✅ checks null as true
  • ✅ checks undefined as true
  • ✅ checks 0 as false
  • ✅ checks as false
  • ✅ checks false as false

Source ​

src/guard/is-nullish.ts (on GitHub)

Source code
ts
/**
 * Checks whether a value is `null` or `undefined`: the opposite of `isDefined`.
 *
 * @param value - The value to check.
 * @returns `true` for `null` and `undefined`; `false` for `0`, `''` and `false`.
 * @example
 * isNullish(undefined); // true
 * isNullish(0); // false
 */
export function isNullish(value: unknown): value is null | undefined {
  return value === null || value === undefined;
}
Test file
ts
import { isNullish } from './is-nullish';

describe(isNullish, () => {
  it.for([
    [null, true],
    [undefined, true],
    [0, false],
    ['', false],
    [false, false],
  ] as const)('checks %s as %s', ([value, expected]) => {
    expect(isNullish(value)).toBe(expected);
  });
});

Keywords: is, nullish, guards, function

Generated from the JSDoc of the sources.