javascript – && and || with different data types

Question:

I just started learning JavaScript and solving problems.

And we began to come across such tasks as:
• What is 2 && 1 && null && 0 && undefined?
• What is 0 || "" || 2 || undefined || true || falsе?

How can different data types be compared?

Answer:

If we refer to the specification :

LogicalANDExpression : LogicalANDExpression && BitwiseORExpression
1. Пусть lref результат вычисления LogicalANDExpression.
2. Пусть lval будет результатом GetValue(lref).
3. Пусть lbool результат ToBoolean(lval).
4. Если lbool - false, вернуть lval.
5. Пусть rref это результат вычисления BitwiseORExpression.
6. Вернуть GetValue(rref).

In the case of && left operand is bool to bool and if false , the operand itself is returned. Otherwise, the right-hand operand is returned.

LogicalORExpression : LogicalORExpression || LogicalANDExpression
1. Пусть lref результат вычисления LogicalORExpression.
2. Пусть lval будет результатом GetValue(lref).
3. Пусть lbool результат ToBoolean(lval).
4. Если lbool - true, вернуть lval.
5. Пусть rref это результат вычисления LogicalANDExpression.
6. Вернуть GetValue(rref).

In the case of || the left operand is bool to bool and if true the operand itself is returned. Otherwise, the right-hand operand is returned.

Scroll to Top