HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

JavaScript Logical Operations

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptoperationslogical

Problem

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.
The logical and (&&) operator returns true if both operands are true, otherwise it returns false.
| a | b | a && b |
| ----- | ----- | ------ |
| true | true | true |

Solution

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

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


| a | b | a && b |
| ----- | ----- | ------ |
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |

Code Snippets

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

and(true, true); // true
and(true, false); // false
and(false, false); // false
const or = (a, b) => a || b;

or(true, true); // true
or(true, false); // true
or(false, false); // false
const not = a => !a;

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

Context

From 30-seconds-of-code: logical-operations

Revisions (0)

No revisions yet.