Logical complement

JavaScript, Function, Logic · Sep 15, 2020

Returns a function that is the logical complement of the given function, fn.

  • Use the logical not (!) operator on the result of calling fn with any supplied args.
const complement = fn => (...args) => !fn(...args);

const isEven = num => num % 2 === 0;
const isOdd = complement(isEven);
isOdd(2); // false
isOdd(3); // true

More like this

  • JavaScript Logical Operations

    Get started with logical operations in JavaScript with this small snippet collection.

    Collection · 9 snippets

  • Logical or for functions

    Checks if at least one function returns true for a given set of arguments.

    JavaScript, Function · Oct 19, 2020

  • Bind function context

    Creates a function that invokes fn with a given context, optionally prepending any additional supplied parameters to the arguments.

    JavaScript, Function · Oct 18, 2020

  • Cycle generator

    Creates a generator, looping over the given array indefinitely.

    JavaScript, Function · Oct 11, 2020