snippetjavascriptTip
JavaScript Logical Operations
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 (
| a | b | a && b |
| ----- | ----- | ------ |
| true | true | true |
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); // falseconst or = (a, b) => a || b;
or(true, true); // true
or(true, false); // true
or(false, false); // falseconst not = a => !a;
not(true); // false
not(false); // trueContext
From 30-seconds-of-code: logical-operations
Revisions (0)
No revisions yet.