1

I know it is very much stupid to ask but can anyone tell me Why === and == giving false for following.

x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);

Here typeof(x[0]) and typeof([1,2]) is also same, then why it is giving false?

yajiv
  • 2,901
  • 2
  • 15
  • 25
  • Read up on Sameness Comparisons here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness – Marie Mar 14 '18 at 14:38
  • Duplicate => https://stackoverflow.com/questions/30820611/why-doesnt-equality-check-work-with-arrays – th3n3wguy Mar 14 '18 at 20:42

1 Answers1

5

Because they are different values in memory.

x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory

var y = x[0]; //Same value in memory

console.log(x[0]===y);
console.log(x[0]==y);

Equality comparisons and sameness

Ele
  • 33,468
  • 7
  • 37
  • 75