Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

At the end of https://leanpub.com/javascript-allonge/read#leanpub-auto-val..., it states that when you do this:

  var ouroboros = [];
  ouroboros[0] = ouroboros;
    //=> [ [Circular] ]
Examining ouroboros and ouroboros[0] with '===' will show that they are different.

But isn't that wrong? Comparing a reference of 'ouroboros' to whatever's in 'ouroboros[0]', which happens to be a reference of 'ouroboros' will return true.

Maybe I'm misinterpreting the statement?



It looks like this text is about the section slightly above:

  [2-1, 2, 2+1] === [1,2,3]
  [1,2,3] === [1, 2, 3]
  [1, 2, 3] === [1, 2, 3]


Aah, rereading it, it looks like it is. The ouroboros section is an aside about how confusing combining arrays and references can be.


Just a guess -- perhaps

    ouroborous[0] = ouroboros;
creates a new reference to ouroboros and stores that in ouroboros[0], so comparing ouroboros and ouroboros[0] with === would claim they're the same type but not the same value.

Not an expert though, so I could be wrong, but that'd explain the behavior to me.


Yes, I don’t understand what he means either.

    ouroboros[0] === ouroboros
evaluates to `true`.





Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: