# JavaScript Logical Operations

Boolean logic and logical operations might not come up that often in JavaScript development, but when they do, you'll be glad you know how to use them. Let's explore some of the most common logical operations in JavaScript and how to implement them in a functional style.

## Logical AND

The logical and (`&&`) operator returns `true` if both operands are `true`, otherwise it returns `false`.

```const and = (a, b) => a && b;

and(true, true); // true
and(true, false); // false
and(false, false); // false```

## Logical OR

The logical or (`||`) operator returns `true` if at least one of the operands is `true`, otherwise it returns `false`.

```const or = (a, b) => a || b;

or(true, true); // true
or(true, false); // true
or(false, false); // false```

## Logical NOT

The logical not (`!`) operator returns `true` if the operand is `false`, otherwise it returns `false`.

```const not = a => !a;

not(true); // false
not(false); // true```

## Logical XOR

The logical xor (`^`) operator returns `true` if exactly one of the operands is `true`, otherwise it returns `false`.

```const xor = (a, b) => (( a || b ) && !( a && b ));

xor(true, true); // false
xor(true, false); // true
xor(false, true); // true
xor(false, false); // false```

## Logical NOR

The logical nor operator is a combination of the logical not and logical or operators. It returns `true` if none of the operands are `true`, otherwise it returns `false`.

```const nor = (a, b) => !(a||b);

nor(true, true); // false
nor(true, false); // false
nor(false, false); // true```

## Logical NAND

The logical nand operator is a combination of the logical not and logical and operators. It returns `true` if at least one of the operands is `false`, otherwise it returns `false`.

```const nand = (a, b) => !(a&&b);

nand(true, true); // false
nand(true, false); // true
nand(false, false); // true```

## Implementing logical operations with functions

The logical operations we've seen so far work with boolean values, but what if we want to use them with functions? For example, what if we want to check if both of two functions return `true` for a given set of arguments? We'll have to alter them in such a way that they work with functions. For example, here's the implementation of the logical and, or, and not operators for functions:

```// Logical and for functions
const both = (f, g) => (...args) => f(...args) && g(...args);
// Logical or for functions
const either = (f, g) => (...args) => f(...args) || g(...args);
// Logical not for functions
const complement = fn => (...args) => !fn(...args);```

In fact, we can generalize this to implement any binary logical operator for functions, using higher-order functions.

```const logicalOperator =
operatorFn =>
(f, g) =>
(...args) =>
operatorFn(f(...args), g(...args));

const and = (a, b) => a && b;
const or = (a, b) => a || b;

const both = logicalOperator(and);
const either = logicalOperator(or);

const isEven = num => num % 2 === 0;
const isPositive = num => num > 0;
const isPositiveAndEven = both(isEven, isPositive);
const isPositiveOrEven = either(isEven, isPositive);

isPositiveAndEven(4); // true
isPositiveAndEven(-2); // false
isPositiveOrEven(4); // true
isPositiveOrEven(-2); // true```

## More like this

• Collection · 56 snippets

### JavaScript Function Snippets

Learn everything about JavaScript functions with this ES6 snippet collection.

• JavaScript ·

### Formatting numeric values in JavaScript

Learn common number formatting operations, such as rounding, padding, optional decimal marks, currency, seconds, bytes, and more.

• JavaScript ·

### Implement the SDBM hash function in JavaScript

SDBM is a simple, non-cryptographic hash function that can hash strings into whole numbers. Here's a JavaScript implementation.

• JavaScript ·

### Create a unit converter data structure in JavaScript

Learn how to create a unit converter data structure in JavaScript that can convert between any compatible units.

Start typing a keyphrase to see matching snippets.