Computer >> Computer tutorials >  >> Programming >> Javascript

What types of logical operators are in javascript?


Logical Operators

Logic operators are used to find the logic between variables in JavaScript.There are three logical operators in JavaScript: || (OR), && (AND), ! (NOT).

The AND operator

The AND operator (&&) returns true if both expressions are true, otherwise it returns false.

Example

<html>
<body>
<p id="and"></p>
<script>
   var a = 200;
   var b = 300;
   document.getElementById("and").innerHTML =
   (a > 100 && b <500) + "<br>" +
   (a < 100 && b <50);
</script>
</body>
</html>

Output

true
false

The OR operator

The OR operator gives value true if one or both expressions are true.

Example

<html>
<body>
<p id="or"></p>
<script>
   var x = 200;
   var y = 300;
   document.getElementById("or").innerHTML =
   (x == 200 || y == 300) + "<br>" +
   (x == 200 || y == 0) + "<br>" +
   (x == 0 || y == 0)
</script>
</body>
</html>

Output

true
true
false

The NOT operator

The NOT operator gives false for true values and true for false values.

Example

<html>
<body>
<p id="not"></p>
<script>
   var x = 200;
   var y = 300;
   document.getElementById("not").innerHTML =
   !(x < y) + "<br>" + !(x > y);
</script>
</body>
</html>

Output

false
true