If does not when comparing with array

Hi all. I wonder about this in executing a function with if:

Function A (value){
If(value ==){
Return true;
} else return false;

It always return false even if I passed value as .
Anyone can enlighten me? Since I am a beginner in Java so I do hope a simple explanation. Thank you so much.

The == operator does not read arrays.

1 Like

try to change the == to === this might work , because when you use == it compares the data type not the value of the variable . Hopefully this makes sense

I tried “===” before but the same outcome. I think the root cause as nhcarrigan mentioned, “==” does not read arrays.

=== requires the value and data type to be the same.
== only requires the value to be the same. It will convert the data type when it can.

1 Like

Thank you, @nhcarrigan. Might you help me with which method or what I can do in this situation to express: if passed argument returns an empty array, it will return true?

To make sure I am understanding correctly, you want function A([]) to return true, and any other argument to return false?

1 Like

Yes, it’s correct. That’s what I want.

comparing arrays doesn’t work with comparison operator
you need to

  1. check if it is an array
  2. check if it is an empty array

can you imagine how to do that?

for comparing arrays, you can’t use comparison operator. You can check by doing something like [1, 2, 3] === [1, 2, 3], it will return false. Weird, right? but that’s how it behaves

A different way would be to check by stringifiying, like JSON.stringify([1, 2, 3]) === JSON.stringify([1, 2, 3]), this return true.