I was working on my chat app and got this error message in the terminal, along with the code I wrote:
5:49:54 PM [vite] warning: Comparison using the "!==" operator here is always true
178| }, [receiverID]);
179| useEffect(() => {
180| if (textConversation !== []) {
| ^
181| const lastMessage = textConversation[textConversation.length - 1];
182| getReceipts(lastMessage["id"]);
In this code snippet, why would the !==
operator always be true?
That is not how you check for an empty array. You can check the length. Also, an array literal is never the same reference as a different array.
[] === []
// false
const array = [];
array === []
// false
array.length === 0;
// true
1 Like
Also, an array literal is never the same reference as a different array.
Can you give some more explanation as to why this is the case?
A JavaScript variable only holds one piece of information. So for arrays and objects, that one piece of information is the location where the array or object is stored. Different arrays are stored in different locations.
A JavaScript variable only holds one piece of information. So for arrays and objects, that one piece of information is the location where the array or object is stored. Different arrays are stored in different locations.
@JeremyLT Is this the reason why an empty array is not the same as another empty array?