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/function/negate.md.

negate es-toolkit

Creates a new function that inverts the return value of a function that returns true or false.

const negatedFunc = negate(booleanFunc);

Usage

negate(func)

Use negate when you want to invert the result of a function that returns true or false.

This is useful for inverting conditional functions or filtering logic. For example, you can turn a function that finds even numbers into a function that finds odd numbers.

import { negate } from 'es-toolkit/function';

// Basic usage
const isEven = (n: number) => n % 2 === 0;
const isOdd = negate(isEven);

console.log(isEven(2)); // true
console.log(isOdd(2)); // false

console.log(isEven(3)); // false
console.log(isOdd(3)); // true

// Using in array filtering
const numbers = [1, 2, 3, 4, 5, 6];

const evenNumbers = numbers.filter(isEven);
console.log(evenNumbers); // [2, 4, 6]

const oddNumbers = numbers.filter(negate(isEven));
console.log(oddNumbers); // [1, 3, 5]

You can also invert complex conditional functions.

import { negate } from 'es-toolkit/function';

const isLongString = (str: string) => str.length > 5;
const isShortString = negate(isLongString);

const words = ['hi', 'hello', 'world', 'javascript'];

const longWords = words.filter(isLongString);
console.log(longWords); // ['javascript']

const shortWords = words.filter(isShortString);
console.log(shortWords); // ['hi', 'hello', 'world']

Parameters

funcFrequired

A function that returns a boolean value.

Returns

Returns a new function that accepts the same arguments as the original function but returns the opposite boolean value.

F
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.