JS Operators Part Three
Last updated
Was this helpful?
Last updated
Was this helpful?
Logical operators are used to compare values. Any type of values can be compared using logical operator.
Three logical operators in Javascript are:
OR ||
AND &&
NOT !
Lets dive into it
Represented with two pipe characters or two vertical lines and called as disjunction
The || operator represents
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
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.
syntax: ||
Returns true, if any one operand is true
Returns false, only if every operands are false
0, false, empty string, null, undefined are false
OR operator seeks first truthy value
Operation stops once a value is evaluated as true, and return the original value
If all values are evaluated as false, then it returns the last original value
Represented with two ampersands and called as conjunction
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
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.
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.
syntax: &&
Returns false, if any one operand is false
Returns true, only if every operands are true
0, false, empty string, null, undefined are false
AND operator seeks first falsy value
Operation stops once a value is evaluated as false, and returns the original value
If all values are evaluated as true, then it returns the last original value
Represented with exclamation and called as Negation
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.
It means true and !!true are equivalent
See how it works..
NOT is a unary operator
Returns the inverse value, if value is true, NOT returns false and vice-versa
Double negation reverses the inverse
The && operator represents
The ! operator represents