0

I read over a doc on equality comparison at MDN, but I'm interested in how Javascript performs strict equality checking.

There's also this specification: http://ecma-international.org/ecma-262/5.1/#sec-11.9.6, though, I don't really understand how it applies to two objects. The last statement is the key I think:

Return true if x and y refer to the same object. Otherwise, return false.

But how does Javascript check if they refer to the same object?

For example, given an object Kitten that has a name property, I create two kitties initialized with their names:

var kittenA = new Kitten("kitty A");
var kittenB = new Kitten("kitty B");

What does Javascript use to determine that that the following statement

kittenA === kittenB

Will return false?

9
  • You might be interested in How to explain object references in ECMAScript terms?. I don't think there's much else behind it. Commented Apr 28, 2016 at 16:31
  • Your title question sounds a bit like a duplicate Difference between == and === in JavaScript. You should edit it to make clear that your question is limited to objects. Commented Apr 28, 2016 at 16:35
  • It's checking whether the references are identical (i.e. both kittenA and kittenB point to the same underlying object). In this case the references point to different objects which is why it returns false. Commented Apr 28, 2016 at 16:38
  • @Bergi I have updated the title. I also looked over the answer, but I'm not sure which part would help me (or someone) understand how javascript checks an object's reference. Commented Apr 28, 2016 at 16:39
  • 1
    @MxyL: Both V8 and Gecko are open source, so yes you can look it up if you're interested. An object reference is typically just a (possibly tagged) pointer to some block of memory that contains data of and about the object. Commented Apr 28, 2016 at 16:51

1 Answer 1

3

kittenA and kittenB are both of type object so case 7 applies:

  1. Return true if x and y refer to the same object. Otherwise, return false.

How does the engine know they are two references to the same object? How a reference is implemented exactly will vary per engine. It might just be a number pointing to a memory location or an index in a list (of all objects).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.