Skip to content

isNull function ​

Checks whether a value is null; undefined does not pass.

ts
import { isNull } from 'utils';

Signature ​

ts
export function isNull(value: unknown): value is null

Parameters ​

NameDescription
valueThe value to check.

Returns ​

true for null only.

Example ​

ts
isNull(null); // true
isNull(undefined); // false

Tests ​

Line coverage: 100 %.

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

Source ​

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

Source code
ts
/**
 * Checks whether a value is `null`; `undefined` does not pass.
 *
 * @param value - The value to check.
 * @returns `true` for `null` only.
 * @example
 * isNull(null); // true
 * isNull(undefined); // false
 */
export function isNull(value: unknown): value is null {
  return value === null;
}
Test file
ts
import { isNull } from './is-null';

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

Keywords: is, null, guards, function

Generated from the JSDoc of the sources.