I am doing some practice in chrome console
>>var a = 2;
>> a || 3
2
and
>>a && 3
3
Why and how... isn't the output should be true or false, please explain, maybe I am wrong, I think these operator should return true or false.
The logical AND (&&) operator evaluates its right operand if lVal is a truthy[2]
value.
Analogous, the logical OR (||) operator evaluates its right operand if lVal is a falsy[1] value.
So, citing the ES5 Specification
[...] The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions. (ES5 §11.11)
From the MDN Article Logical Operators
Operator | Usage | Description
_____________________________________________________________________________________________________
Logical AND (&&) | expr1 && expr2 | Returns expr1 if it can be converted
| | to false; otherwise, returns expr2.
| | Thus, when used with Boolean values,
| | && returns true if both operands are true; otherwise, returns false.
_________________________________________________________________________________________________
Logical OR (||) | expr1 || expr2 | Returns expr1 if it can be converted to true; otherwise, returns expr2.
| | Thus, when used with Boolean values,
| | || returns true if either operand is true; if both are false, returns false.
[1] A value is considered falsy if it can be converted to false.
That applies to the values false,0,"",null,undefined,NaN
[2] A truthy value is any value that is not falsy[1]
This is no different to what happens if you type a single number into the console, or anything else for that matter. You get back the last thing to be evaluated.
You're effectively saying (in pseudocode, not JS)
[return value] = 2 or 3
2 evaluates to true, so there's no need to evaluate 3 (short-circuit evaluation). The last thing to be evaluated was 2, so you get 2.
[return value] = 2 and 3
2 is true so evaluate 3 as well. The last thing to be evaluated is 3, so you get 3
This is explained well on the Mozilla Developer Network.
When you have a || b JavaScript evaluates the expression and returns the first value which is being evaluated to true, in your case 2 (one of the reasons for that is the lazy evaluation).
In the second example a && b JavaScript needs to evaluate both - a and b, in order to make sure the conjunction is true. This way JavaScript returns the last thing evaluated to true - 3. If the expression is being evaluated to false the interpreter will give you false instead (this will happen if any of the operands is being evaluated to false).
if you check a || b and a is converted as a truthy value, this expression will return the first true operand because it's enough to evaluate an OR expression, without go ahead in the evaluation (2 || 3 will return 2, but 0 || 3 will return 3 since zero is considered as a falsy value)
but if you check 2 && 3 you must be sure that both the operands are true, so javascript will evaluate them all and will return last operand evaluated (if you check 3 && 0 && 2 the expression will return 0 because this operand is false and — as AND expression — there's no need to evaluate further)
This behaviour explains why || and && are also called short circuit boolean operators
if you want instead to have the boolean result of the whole expression just try in this way
!!(<expression>)
Example:
!!(2 || 3) // true
!!(0 || 3) // true
!!(2 && 0) // false
!to coerce the number0,resulting from the bitwise operation&, to a boolean. The result of the bitwise operator itself is not a boolean. This could be applied to the OP's question too.!!(2 || 3) //true