Why the output of the third comparison is false?


I am encountering a problem here:
`let a = [1, 2, 3];
let b = [1, 2, 3];
let c = "1,2,3";

console.log(a == c);
console.log(b == c);
console.log(a == b);`

The output was:
true true false

Both variable a and b are having the same value declared, but why a == b is false?

>Solution :

Because == operator performs type coercion.

The == operator will only returns true if both refer to the same object in memory.

In JavaScript, arrays are objects, a and b are just having same values but they are two separate arrays (different object in memory).

You may check this article to know more about array comparison methods.

Leave a Reply Cancel reply