In this guide, you will learn all about JavaScript logical operators with the help of examples. In the previous tutorial, we have seen JavaScript comparison operators along with examples.
Before going through this article, we will know about Operator.
Headings of Contents
What is Operator?
Operator is a special symbol that perform operation on operands. Operands can be values and variables.
JavaScript Logical Operators
JavaScript logical operators are used to perform logical operations on variables or values. Logical operators in JavaScript are AND ( && ), OR ( || ) and NOT ( ! ).
Operator | Description | Example |
---|---|---|
&& | Logical AND | ( 5 < 10 && 10 > 8) is true |
|| | Logical OR | ( 5 < 10 || 10 < 8) is true |
! | Logical NOT | !( 5 == 5 ) is false |
Logical AND ( && )
Logical AND operators return true, if both of the operands separated by and ( && ) operator are true, otherwise returns false.
Example:
const a = 12, b = 20, c = 12;
console.log((a > 5) && (a == c)); // true
console.log((a == c) && (a > b)); // false
Logical OR ( || )
Logical OR ( || ) operator returns true if anyone condition separated by OR operator is true. otherwise, return false.
Example:
const a = 12, b = 20, c = 12;
console.log((a > b) || (a > c)); // false
console.log((a == c) || (a > b)); // true
Logical NOT ( ! )
Logical NOT ( ! ) operator evaluate true, if the operand is false and vice-verca.
const a = true, b = false;
console.log(!a); // false
console.log(!b); // true
Conclusion
Here, you have learned all about logical operators in JavaScript with the help of examples. JavaScript logical operators are always used to performed logical operations on variables or values. If you have two or more conditions then you can connect them with logical operators.
If you like this article, please share and keep visiting for further JavaScript tutorials.