This question has already been answered:

I read about type conversion in JavaScript. I did not understand what was happening in the following cases:

alert(5 && 2); // 2 alert(2 && 5); // 5 alert(5 || 0); // 5 

At first I thought that there would be a conversion to a logical type and further operations already with the transformed data. But no. Another option is a logical operation, the operation works as a bitwise one, but something is also not right.

Tell me exactly what happens in these examples?

Reported as a duplicate at Grundy. javascript Oct 12 '18 at 15:28 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • @Grundy, well, you chose the question ... In the same place, the accepted answer is tin. Maybe the opposite will close? - Qwertiy
  • @Qwertiy, accepted yes, a strange choice, but there are two more answers with explanations - Grundy
  • @Grundy, well, can the answer be rewritten here, and can it be closed? - Qwertiy

2 answers 2

At first I thought that there would be a conversion to a logical type and further operations already with the transformed data. But no.

Actually yes. Comparison of operands is really made only after they are reduced to a logical type. Here only returns not true or false , but directly one of the operands.

Here is what is written in the specification about this:

The value produced by a && or || operator is not necessarily the type of Boolean. The one of the two operand expressions.

In section 12.13.3 , the algorithms themselves are given (about them in more detail below) the operations of these operators ( || and && ), from which it is clear that the operands are indeed converted to a boolean type.

Here the main difficulty may arise with the cast itself, but in the latest revision of the specification a very convenient label appeared in which it is written in a human-comprehensible form what values ​​are brought to it. I advise you to read, if necessary.

Return value

This is not so obvious. Although the algorithms given by the specification are exhaustive, I think their explanation will be useful here.

&& operator
If the left operand is converted to false , then it is returned. Otherwise, the right operand is returned:

 // Везде вернется левый операнд, поскольку он преобразуется к `false`. // И даже если и правый операнд тоже преобразуется к false, то все равно будет возвращен левый (случаи 4-6). let examples = [0 && true, false && "string", "" && 82, 0 && false, false && "", "" && 0]; examples.forEach((example) => { console.log(example); }); 

Operator ||

Here, the left operand will always be returned in reverse only if it is cast to true :

 // Если левый операнд приводится к true, то он же и возвращается. // И даже если и правый операнд тоже приводится к true, то все равно возвращается левый (случаи 3 и 4). // Однако если ни один из них не приводится к true, то возвращается последний (правый) (случаи 5 и 6). let examples = [true || false, "string" || "", true || true, 1 || [], false || 0, 0 || ""]; examples.forEach((example) => { console.log(example); }); 

    According to MDN :

    expr1 && expr2 returns the value of expr1 if it can be converted to false; otherwise returns the value of expr2

    Since expr1 ( 5 ) is converted to true in the first example, it returns expr2 ( 2 ). Similarly with the second example

    expr1 || expr2 expr1 || expr2 returns the value of expr1 if it can be converted to true; otherwise returns the value of expr2

    In the third example, expr1 ( 5 ) is converted to true, so it returns