Learn JavaScript

JavaScript Logical Operators

JavaScript Logical Operators are used to perform some logical operations. The fundamental logical operators in JavaScript are logical AND (&&), logical OR (||), and logical NOT (!). Although these operators are called logical, still they are not restricted to apply only on Boolean . In fact, they can be applied to any type of values or operands. These operators are generally used to determine the logic between the values or variables.

In order to understand the working of logical operators, let us take a and b are two variables with initial values 3 and 7 respectively.

OperatorsDescription
&&True, when both operands are true, otherwise false
||False, when both operands are false, otherwise true.
!True, when the operand is false and vice versa

Example on JavaScript Logical operators

<html>
<body>
<script>
function jlo(){
var a = true, b = false;
// JavaScript Logiacal AND && Operator
document.write(a&&a);
document.write("<br>");
document.write(a&&b);
document.write("<br>");
document.write(b&&b);
document.write("<p>");
// JavaScript Logical OR || Operator
document.write(a||a);
document.write("<br>");
document.write(a||b);
document.write("<br>");
document.write(b||b);
document.write("<p>");
// JavaScript Logical NOT ! Operator
document.write(!a);
document.write("<br>");
document.write(!b);
}
jlo();
</script>
</body>
</html>

Output

true
false
false

true
true
false

false
true


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.