JavaScript Logical Operators

JavaScript Logical Operators are used to create boolean conditions, modify a boolean expression, or combine two or more simple conditions to form a complex condition.

Logical Operator – Symbol – Example

JavaScript supports the following logical operators.

Logical OperationOperator SymbolExample
AND&&a && b
OR||a || b
NOT!!a

Tutorials

The following JavaScript tutorials cover each of these logical operators in detail.

Example

In the following example, we take two boolean values, and perform different Logical Operations on these values.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var a = true;
        var b = false;
        var and = a && b;
        var or = a || b;
        var not = !a;

        var displayOutput = '';
        displayOutput += '\n' + 'a && b = ' + and;
        displayOutput += '\n' + 'a || b = ' + or;
        displayOutput += '\n' + '!a     = ' + not;
        document.getElementById('output').innerHTML = displayOutput;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned about Logical Operators, their syntax, and usage with examples.