There are four logical operators in JavaScript: Show
Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type. Let’s see the details. || (OR)The “OR” operator is represented with two vertical line symbols: In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are In JavaScript, the operator is a little bit trickier and more powerful. But first, let’s see what happens with boolean values. There are four possible logical combinations:
As we can see, the result is always If an operand is not a boolean, it’s converted to a boolean for the evaluation. For instance, the number
Most of the time, OR For example:
We can pass more conditions:
OR "||" finds the first truthy valueThe logic described above is somewhat classical. Now, let’s bring in the “extra” features of JavaScript. The extended algorithm works as follows. Given multiple OR’ed values:
The OR
A value is returned in its original form, without the conversion. In other words, a chain of OR For instance:
This leads to some interesting usage compared to a “pure, classical, boolean-only OR”.
&& (AND)The AND operator is represented with two ampersands In classical programming, AND returns
An example with
Just as with OR, any value is allowed as an operand of AND:
AND “&&” finds the first falsy valueGiven multiple AND’ed values:
The AND
In other words, AND returns the first falsy value or the last value if none were found. The rules above are similar to OR. The difference is that AND returns the first falsy value while OR returns the first truthy one. Examples:
We can also pass several values in a row. See how the first falsy one is returned:
When all values are truthy, the last value is returned:
Precedence of AND The precedence of AND So the code Don’t replace Sometimes, people use the AND For instance:
The action in the right part of So we basically have an analogue for:
Although, the variant with ! (NOT)The boolean
NOT operator is represented with an exclamation sign The syntax is pretty simple: The operator accepts a single argument and does the following:
For instance:
A double NOT
That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. There’s a little more verbose way to do the same thing – a built-in
The precedence of NOT What is the output of logical or operator if one of the inputs operands is false?Logical OR operator give true as output if at least one of the operands is true. 13) What is the output of Logical AND (&) operation if one of the inputs/operands is false? Explanation: false & (anything) is false.
What is the output of an Exclusive OR operation if one of the operands is false?4) What is the output of an Exclusive OR(^) operation, if one of the operands is false? Explanation: Exclusive OR (^) gives an output of true if both the operands are different. If both are the same (true / false), the output is false.
What is the output of an or operation if one of the operands or expression is true?|| (Logical OR) operator
If one of the operands or expressions is true, it will return 1. If all of them are false, it will return 0.
What is the output of a bitwise AND operation if one of the inputs operands is 0?The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. In C Programming, the bitwise AND operator is denoted by & . Let us suppose the bitwise AND operation of two integers 12 and 25.
|