DEV Community

Cover image for ๐Ÿ”ฅ Inverting Boolean functions in JavaScript
Dom Habersack
Dom Habersack

Posted on • Originally published at islovely.co

๐Ÿ”ฅ Inverting Boolean functions in JavaScript

In JavaScript, we can invert Boolean values with an exclamation mark. That doesnโ€™t work for function names we use as shorthand in array methods like Array.prototype.filter() and Array.prototype.map(). Wrap those in a helper function to have them return the opposite of what they would return normally.

const numbers = [0, 1, 2, 3, 4, 5]
const isEven = n => n % 2 === 0

// the long and short form of this do the same
numbers.filter(number => isEven(number))  // โ‡’ [0, 2, 4]
numbers.filter(isEven)                    // โ‡’ [0, 2, 4]

// `!` can flip the Boolean value, but it only works with the long form
numbers.filter(number => !isEven(number))  // โ‡’ [1, 3, 5]
numbers.filter(!isEven)                    // TypeError (not a function)

// this (curried) helper makes functions return a flipped result
const not = callback => value => !callback(value)

// we can use `not` like this, in both the long and short form
numbers.filter(number => not(isEven)(number))  // โ‡’ [1, 3, 5]
numbers.filter(not(isEven))                    // โ‡’ [1, 3, 5]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)