Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading