0

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.

6
  • 2
    Why do you think they should return true or false? Have a look here Commented Mar 13, 2014 at 9:18
  • I think because he's used to see them in an "if", which returns true or false Commented Mar 13, 2014 at 9:19
  • Be sure you're not confusing the operators with their bitwise counterparts. Commented Mar 13, 2014 at 9:24
  • @Nit How would a bitwise operator result in a boolean? Commented Mar 13, 2014 at 9:41
  • 1
    @Nit You are using the logical NOT operator ! to coerce the number 0,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 Commented Mar 13, 2014 at 9:58

5 Answers 5

2

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]

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

Generally speaking,

logical operators return boolean when all elements of the equation are boolean type

Otherwise they work like

// b assign a. If !a then assign 3
var b = a || 3;

// b assign a but only if it would evaluate to false. Otherwise assign 3
var b = a && 3;

Comments

0

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).

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.