0

Why

let a = [1, 2, 3];
console.log(a === [1, 2, 3]);

is "false" in JavaScript?

Esko
  • 4,109
  • 2
  • 22
  • 37
이건창
  • 1
  • 1

2 Answers2

3

Javascript Objects are a bit like C pointers.

a contains the memory address of the first array you define.

When you do console.log(a === [1, 2, 3]); you are in fact creating a new array, and you compare its memory value with the one you kept in a.

That's why:

const a = []; 
a.push(1)

is valid (the constant is the "pointer", not the array)

Vico
  • 1,696
  • 1
  • 24
  • 57
0

Because the arrays aren't the same. The variable a contains a different array than the one in the console.log even though the array values are the same. You would have to loop through both arrays and compare each value of both to determine if they were equal.

James Coyle
  • 9,922
  • 1
  • 40
  • 48