1
        var array1=[1,2,3];
        var array2=[1,2,3];
        alert((array1<array2)+(array1==array2)+(array1>array2));

As alert returns 0, array1 is not greater, not less and not equal to array2.
The question is:
How does array1 relates to array2?

Edit: my question is: How does array1 relates to array2?

2

2 Answers 2

4

The two array array1 and array2 are never equal as they are different instances.

If you want to compare them, you can do:

array1.join() == array2.join() // true

And BTW, the alert() doesn't alert false it alerts 0

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

Comments

0

Weird thing about arrays, they are never equal since they're not the same object.

Although, arrays act strange when you compare them with < or >

We can use this weird behavior to our advantage to check whether two arrays have the same content:

function arraysEqual(a, b) { return !(a < b || b < a); }

Or

function arraysEqual(a, b) { return !(a < b) && !(b < a); }

Which means, if the no array is bigger than the other, they're equal.


Source: How to check if two arrays are equal with JavaScript?

2 Comments

array1 is not greater, not less and not equal to array2
As I said, if the no array is bigger than the other, they're equal. You are welcome to test the function I have given and check the results.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.