0

I'm trying to understand the following output:

'use strict';
console.log(Object.getPrototypeOf([]));
// [] 
console.log(Object.getPrototypeOf(Array.prototype));
// {}
console.log(Object.getPrototypeOf([]) == Array.prototype);
// true
console.log(Object.getPrototypeOf([]) === Array.prototype);
// true
console.log([]=={});
// false
console.log([]==={});
// false

In particular, why are lines 6 and 8 evaluated to be true, whereas lines 10 and 12 are evaluated to be false.#

Edit: I made a stupid typo on lines 6 and 8, which I have now edited. This makes the question different. Apologies.

Hunter
  • 357
  • 8
  • 15
  • Possible duplicate of [What’s the difference between “{}” and “\[\]” while declaring a JavaScript array?](https://stackoverflow.com/questions/33514915/what-s-the-difference-between-and-while-declaring-a-javascript-array) – Maximilian Ballard Aug 26 '18 at 10:26
  • Here are 2 algorithms used for `==` and `===` comparisons: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 and http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6. It may help understand differences. – Andriy Aug 26 '18 at 10:52

3 Answers3

3

All prototypes in Javascript eventually inherit from the same object prototype. That's why if you will add a property to the object prototype, all the objects in your program will end up having this property. In lines 6 and 8 you are comparing Object.prototype to itself, which is equal.

However in lines 10 and 12, you are comparing 2 different objects, they may share the same prototype, but these are completely two different objects in memory.

Lihai
  • 323
  • 2
  • 6
  • 18
2

In lines 6 and 8, you are literally comparing two exact same expressions.

(X)===(X) is always1 true for any X, and in that case (X)==(X) is also automatically true (not always the other way round).

[] and {} are not the same, hence the false from line 10 and 12.

(1) assuming of course that X is a non-destructive or read-only expression, if X is something like (Y++) then this doesn't hold.

RocketNuts
  • 9,958
  • 11
  • 47
  • 88
  • Also `{} === {}` or `[] === []` are both false so it's not as easy as `(X)===(X) is always true for any X` – iuliu.net Aug 26 '18 at 10:32
  • @iuliu.net because `[]` is not the same as `x`, but more like `new x()` (it creates a new instance) everytime it is called. If you do `var x = []; x === x` you will get the expected result. – knittl Aug 26 '18 at 10:34
  • Yeah well assuming that `X` is variable sure, alright, it's correct (and of course assuming no proxies were applied). But the answer assumes `X` is an expression, in which case my clarification stands. – iuliu.net Aug 26 '18 at 10:38
  • Note that I added a comment about assuming `X` is a non-destructive or read-only expression. Neither `[]` nor `{}` are read-only expressions. – RocketNuts Aug 26 '18 at 11:13
1

[] will create a new array instance. Two instance are different from each other (you can modify them independently). The same holds for {}, it creates a new object instance.

All array instances share the same prototype instance (there is only a single array prototype instance). The prototype of an array prototype is an object.

They just happen to have the same string/JSON representation.

What you are basically doing in your second console.log example is: console.log(Object.getPrototypeOf(Object.getPrototypeOf([])))

The last two lines return false because you are comparing objects and arrays (which cannot be equal)

knittl
  • 246,190
  • 53
  • 318
  • 364