-2

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?

3
  • 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 Commented Aug 8, 2021 at 8: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. Commented Aug 8, 2021 at 8: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 Commented Aug 8, 2021 at 8:09

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.