# JavaScript Logical Operators

Last Updated : 09 Apr, 2024

## Logical operators

The logical operators are mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. It allows us to compare variables or values.

In JavaScript, there are basically three types of logical operators.

OPERATOR NAMEOPERATOR SYMBOLOPERATION
NOT!Converts operator to boolean and returns flipped value
AND&&Evaluates operands and return true only if all are true
OR||Returns true even if one of the multiple operands is true

## !(NOT)Operator

!(NOT) Operator reverses the boolean result of the operand (or condition). It first converts the operand to a boolean type and then returns its flipped value.

Example: In this example, we will perform the NOT operation on number multiple times.

Javascript ``` // !(NOT) operator let i = 0; console.log((!i)); console.log(!!i); ```

Output:

`truefalse`

Explanation: Since zero is treated as a falsy value therefore NOT operation on zero will return true and when this operation is performed again we get true as output.

## &&(AND) Operator

The && operator accepts multiple arguments and evaluates the operator from left to right. It returns true only if all the operands that are evaluated are true

Example: In this example, we will perform and operation on different numbers and evaluate their output as a boolean

Javascript ``` // &&(AND) operator let i = 0, j=2, k=3, l=8; console.log(Boolean(i&&j&&k)); console.log(Boolean(j&&k&&l)); ```

Output:

`falsetrue`

Explanation: In JavaScript, the value of 0 when converted to zero is considered false so when performing “and” operation on 0 a falsy value is returned and we get false output otherwise true.

## ||(OR) Operator

The OR operator is somewhat opposite of the â€˜ANDâ€™ operator. It also evaluates the operator from left to right and returns true even if one operand is evaluated as true

Example: In this example, we will perform OR operation on different data types and check their boolean output.

Javascript ``` // ||(OR) Operator let i = 1; let j = null; let k = undefined; let l = 0; console.log(Boolean(j||k)); console.log(Boolean(i||l)); ```

Output:

`falsetrue`

Explanation: null, undefined, and 0 are recognized as falsy values. So OR operation on null and undefined will give false whereas numbers except 0 are treated as true.

Supported Browsers: The browsers supported by all JavaScript Logical operators are listed below:

We have a complete list of Javascript Operators, to check those please go through this Javascript Operators Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

Previous
Next