Why is emptyArray == [] returns false while emptyArray.length == 0 returns true?

i tried this in my code and it works well:

var result = []
if (result.length == 0) {
    $("body").append("<div>No result</div>");
  }

however if i change it to:

if (result == []) {

or undefined or null, it wont work

could anyone help explain that why “[]==[]” is false? i remember did that in python and has no problem at all…?

thank you! i’m still a beginner <3

1 Like

If I’m not mistaken, it’s because comparing an array or object with another array or object is always false. This is for reasons not immediately apparent, but I’ve read it’s because arrays can have private data inside.

For instance you can have an array of functions with variables inside the functions. Just like having a function inside of a function (as you may have learned already) the outer function does not have access to the inner functions variables. So an array can have information that cant be accessed by the comparison operator, so it can’t declare it true

Disclaimer: I’m reaaaaallly stretching the limits of my understanding on the above point, so anyone more knowledgeable please correct me if I’m wrong.

Factoid: The following can cause unintended results and is probably frowned upon by most devs (along with using == rather than === in general). You can compare an array to a string, and javascript (if using == not ===) will coerce the array into one string with each item separated by commas, to compare to the other string.

Factoid-Disclaimer: You probably shouldnt do this

eg

arr=[];
console.log(arr=="") //true

or

arr2=[1,2,3];
console.log(arr=="1,2,3") //true

Heres an example of just one problem with this (there are probably many potential problems with this)

function yada(){
  console.log("yadayada")
}
arr2=[yada,"2"];
console.log(arr2=="yada,2") //false

This is called “type coercion” and its a source of contention in Javascript and one of the reasons many people advise always using === over ==

2 Likes

still confused on some details… ; (
but got your last suggestion at least :wink: thank you ~

Equality is a complex topic

To keep it straightforward (not necessarily simple) stick to triple equals === which is strict equality - avoid double equals ==

Now [] === [] is false because there are two distinct empty arrays being compared and strict equality on objects compares the references to the objects not the contents of the objects - similarly [2, 4, 6] === [2, 4, 6] is false

Now what if you have a=[2, 4, 6] and b=[2,4,6] and want to check if a and b have the same values in the same order? You have to go through both arrays comparing values at each index - this is not too bad for numbers though I’d reconsider the approach in my solution before doing this - it gets difficult if the array elements are themselves objects and have to be deep-compared

maybe… if there’s a way to fully “expand” the array (along with its object elements) and parse that thing to a str? (like sometimes you see a bunch of hard-to-read code when you accidently print some object or other things) then you could simply compare 2 strs :flushed:

I guess it could be justified for debugging or in a test framework - in that case just use JSON.stringify

If you think that’s weird, check out this Stack Overflow question (explanation in the top rated answer).

In JS there are two types of values: primitive values (number, string, boolean, null, undefined and symbol), and one type of reference value (objects).

When you assign a primitive value to a variable, that variable refers directly to that single value in the computer’s memory, and when you manipulate it you manipulate it directly. You can compare them because two of the same values are literally the same.

But reference values are a collection of properties. In essence, if you assign an object to a variable, you aren’t referring to a concrete “thing”, you’re setting up a data structure that may contain some primitive values:

var a = 1;
var b = [];
var c = [];
// b does not == c, they're two different things
b.push(a);
c.push(a);
// b and c still have exactly the same structure,
// and both contain the same exact value, but
// they're still not the same
1 Like