JS Operators Part Three

4. Logical Operator

Logical operators are used to compare values. Any type of values can be compared using logical operator.

Three logical operators in Javascript are:

  1. OR ||

  2. AND &&

  3. NOT !

Lets dive into it

1. OR ||

Represented with two pipe characters or two vertical lines and called as disjunction

||

The || operator represents logical or

If any of the operand (argument) is true. It returns true, else it returns false

It means it returns false only if both arguments are false

If an operand or the operands are not boolean, then it is converted to the boolean for evaluation, and returns the original value of the operand, not the converted boolean. We know 1 represents true and 0 represents false. Here in Ex: 2, the numbers are converted to boolean in the operation, but not in the output.

Look at the example below. Ex : 3

What is happening ? Here its a combination of number and boolean

Short-circuit evaluation

The evaluation stops once the operator finds the truthy value.

It evaluates the second operand, only if the first operand is a falsy value, same rule applies to the third operand. In the above example, even before checking the second operator, it returns the true. Operator does not even know what is after the first operand.

Literally the operation stops where it finds a truthy value

OR seeks the first truthy value

It evaluates from left to the right

If the first operand is true, it stops there and returns the original value of that operand

If every operands in the expression are false, it returns the last operand

As OR is seeking to find a true, it stops evaluation until it finds a true. If any value is true, it returns the first true value found. If all values are true, it returns the first true value. If all values are false it returns the last false value

Compare the line numbers to avoid confusion

0, null, undefined, empty string and false are false values. Positive integer, Negative integer, a string and true are true values

-1 is also a true value.

Short-circuit evaluation example

Summary

  1. syntax: ||

  2. Returns true, if any one operand is true

  3. Returns false, only if every operands are false

  4. 0, false, empty string, null, undefined are false

  5. OR operator seeks first truthy value

  6. Operation stops once a value is evaluated as true, and return the original value

  7. If all values are evaluated as false, then it returns the last original value

2. AND &&

Represented with two ampersands and called as conjunction

The && operator represents logical and

Returns true, only if both operands are true, else false

It means it returns false, if any argument is false

Four possible logical boolean combinations

Four possible logical number combinations

Only returns true, when all operands are true

&& operator also follows the short-circuit evaluation method

AND seeks the first falsy value

It evaluates from left to the right

If the first operand is false, it stops there and returns the original value of that operand

If every operands in the expression are true, it returns the original value of the last operand

As AND is seeking to find a false, it stops evaluation until it finds a false. If any value is false, it returns the first false value found. If all values are false, it returns the first false value. If all values are true it returns the last true value

Check the line numbers to avoid confusion

0, null, undefined, empty string and false are false values. Negative integer, positive integer, a string and true are true values.

Now, what is the difference between OR and AND .?

OR always returns true if any true exits, Only returns false if all arguments are false

AND always returns false if any false exits, Only returns true if all arguments are true

OR is a truth seeker like you, AND is a false seeker.

Summary

  1. syntax: &&

  2. Returns false, if any one operand is false

  3. Returns true, only if every operands are true

  4. 0, false, empty string, null, undefined are false

  5. AND operator seeks first falsy value

  6. Operation stops once a value is evaluated as false, and returns the original value

  7. If all values are evaluated as true, then it returns the last original value

3. NOT !

Represented with exclamation and called as Negation

The ! operator represents logical not

Not is a unary logical operator, it means “Not” works with a single argument.

Returns inverse value

It means, if the argument is true, it returns false and if the argument is false, it returns true.

!! Double Negation

It means true and !!true are equivalent

See how it works..

Summary

  1. NOT is a unary operator

  2. Returns the inverse value, if value is true, NOT returns false and vice-versa

  3. Double negation reverses the inverse

Last updated

Was this helpful?