You need to enable JavaScript to run this app.

isNullish

A method that returns true if the accepted value is null or undefined.

Source

export default function isNullish(value: unknown): value is Nullish {
  return typeof value === "undefined" || value === null;
}

What is the Nullish type? See here.

Usage

if (isNullish(value)) {
  // Here, value will be assumed as null or undefined
}

Test

test.each`
  provided     | expected
  ${null}      | ${true}
  ${undefined} | ${true}
  ${false}     | ${false}
  ${true}      | ${false}
  ${""}        | ${false}
  ${NaN}       | ${false}
  ${{}}        | ${false}
`("should be $expected for $provided", ({ provided, expected }) => {
  const mockFn = jest.fn(isNullish);
  const value = mockFn(provided);

  expect(mockFn).toBeCalledTimes(1);
  expect(mockFn).toHaveBeenCalledWith(provided);
  expect(value).toBe(expected);
});
Favourite Books
BooksPoems
Favourite Songs
PlaylistsArtists
Favourite Shows
AnimationsSeriesAnime