Logical Operators in JavaScript

The logical operators in JavaScript are logical AND (&&), logical OR (||), and logical NOT (!) Operator. These operators are used with any of the primitive states or objects. The outcome of these operators will be of any type including Boolean type in the case of JavaScript individually. The operators (&&) and (||) perform more than usual, preferably returning Boolean value every time, they return the value of the operand declared. The operand here can be of the Boolean type or non-Boolean type value.

Types Of Logical Operators in JavaScript:)

  • AND (&&)
  • OR (||)
  • NOT(!)

The logical operators are used to execute logical operations on the values, they are often used with the comparison operators. As usual, the logical operators are used besides the Boolean values true/false in other languages, but in the case of JavaScript, the logical operators can be used for any type of value besides the Boolean values. In JavaScript, these operators perform individually depending upon the values prepared. In this article, we will see three logical operators in JavaScript.

Examples:)

AND (&&) Operator:)

<p id="num"></p>
<script>
var x = 10;
var y = 3;
document.getElementById("num").innerHTML = 
(x < 7 && y > 1) + "<br>" + 
(x < 12 && y < 4);
</script>

OR(||) Operator:)

<p id="num"></p>
<script>
var x = 6;
var y = 3;
document.getElementById("num").innerHTML =
(x == 6 || y == 0) + "<br>" +  
(x == 5 || y == 5);
</script>

NOT (!) Operator:)

<p id="num"></p>
<script>
var x = 6;
var y = 3;
document.getElementById("num").innerHTML = 
!(x === y) + "<br>" + 
!(x > y);
</script>

Conclusion

The logical operators in JavaScript are AND (&&), OR (||), and NOT (!). In JavaScript, these operators can be used in non-Boolean values. In this case, they will return the operand value.

Leave a Reply

Your email address will not be published. Required fields are marked *