To check if an array is empty , we have many choices like if(array.length===0)
or if(array=='')
but I'm wondering why if (array==[])
doesn't check if an array is empty or not. Any one has a clear explanation?
Asked
Active
Viewed 160 times
-2

Peter jackinson
- 13
- 4
-
I was posting an answer that you said you find the answer :). Note that `[]` is new object with new reference and when you use `==` you are checking the reference of them not value of the objects – Alireza Ahmadi Aug 08 '21 at 08:04
-
1@AlirezaAhmadi With 2.2 million JavaScript questions on Stack Overflow, you can almost guarantee that a question like this one has a duplicate. Always search for one before answering. – Ivar Aug 08 '21 at 08:06
-
@Ivar I think OP searched his question before asked. I always focus on the answer. But it seems you are right some OP does not search before asking his question – Alireza Ahmadi Aug 08 '21 at 08:09
1 Answers
0
Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other. and in the same way [] doesn't have any type and if(arr == []) doesn't work in javascript

Sajjan Shah
- 41
- 6