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); });