For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/predicate/isNotNil.md.

isNotNil es-toolkit

Checks if a value is neither null nor undefined.

const result = isNotNil(value);

Usage

isNotNil(value)

Use isNotNil when you want to check if a value is neither null nor undefined. It's particularly useful for filtering out null or undefined values from arrays.

import { isNotNil } from 'es-toolkit/predicate';

// Basic usage
console.log(isNotNil(42)); // true
console.log(isNotNil('hello')); // true
console.log(isNotNil([])); // true
console.log(isNotNil({})); // true

console.log(isNotNil(null)); // false
console.log(isNotNil(undefined)); // false

// Useful for array filtering
const mixedArray = [1, null, 'hello', undefined, true, 0];
const filteredArray = mixedArray.filter(isNotNil);
// filteredArray becomes [1, 'hello', true, 0] (null and undefined removed)

It can also be used as a type guard in TypeScript.

function processItems(items: (string | null | undefined)[]) {
  // Filtering with isNotNil narrows the type to string[]
  const validItems = items.filter(isNotNil);

  validItems.forEach(item => {
    // item is now guaranteed to be of type string
    console.log(item.toUpperCase());
  });
}

Parameters

valueT | null | undefinedrequired

The value to check if it's neither null nor undefined.

Returns

Returns true if the value is neither null nor undefined, false otherwise.

value is T
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.