The MDN documentation says the following about comparison operators:
Features of comparisons:
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding
positions.
- Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything,
including NaN. Positive and negative zeros are equal to one another.
- Two Boolean operands are strictly equal if both are
true
or both are false
.
- Two distinct objects are never equal for either strict or abstract comparisons.
- An expression comparing Objects is only true if the operands reference the same Object.
- Null and Undefined Types are strictly equal to themselves and abstractly equal to each other.
...
If both operands are objects, then JavaScript compares internal references which are not equal when operands refer to different objects in memory.
Objects are compared by reference. An array is also an object. This means [] == []
will always result in false
since they are not the exact same object (with the same reference). This also means [] != []
will always be true
for the same reason.
If you want to check for en empty array use the length
property:
<View>
{recommendations.length ?
(
<Text>Not Empty</Text>
) : (
<Text>Empty</Text>
)}
</View>
This uses the fact that an empty array has length
0
, which is falsy. If you rather be more explicit recommendations.length != 0
would have the same result.