2

I know very well the difference b/w the 2 operators in context to primitive types (number, string, boolean, null and undefined). I mean all that stuff,

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, auto type coercion
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

Consider the snippet,

        var str3 = new String("abc");
        var str4 = new String("abc");

        // false as 2 different objects, referential equality just like in java
        alert(str3 == str4);


        var str5 = new String("abc");
        var str6 = str5;
        alert(str6 == str5);        // true, as pointing to the same object

For str5 and str6, they will compare truthfully. Use of the strict comparison (===) will, of course, produce the same result, as they are the same value and type; indeed, they are the same object!

Now consider,

function Person(name) {
  this.name = name;
}

// the function person has a prototype property
// we can add properties to this function prototype
Person.prototype.kind = ‘person’

// when we create a new object using new
var zack = new Person(‘Zack’);

// the prototype of the new object points to person.prototype
zack.__proto__ == Person.prototype //=> true

zack.__proto__ === Person.prototype //=> false

I find myself really confused with the last 2 lines:

zack.__proto__ == Person.prototype //=> true
zack.__proto__ === Person.prototype //=> false

As per my understanding zack.__proto__ and Person.prototype are pointing to the same object (same location in memory), hence true.

If so, why zack.__proto__ === Person.prototype false then, as the type of both zack.__proto__ and Person.prototype is object and since they are pointing to the same location in memory, they must have equal values.

6
  • 4
    Where is Foo defined? Commented Jan 11, 2015 at 20:31
  • @elclanrs : So Sorry everyone. Edited. Commented Jan 11, 2015 at 20:40
  • 1
    @ShirgillAnsari You should use ' or " instead of . Commented Jan 11, 2015 at 20:40
  • 1
    I can't reproduce you problem. I get true, as expected. Commented Jan 11, 2015 at 20:41
  • 1
    The __proto__ property is non–standard and deprecated, so you shouldn't be using it anyway. Try Object.getPrototypeOf instead. Commented Jan 11, 2015 at 20:51

1 Answer 1

4

If both operands have the same type, it doesn't matter if you use == or ===:

11.9.3 The Abstract Equality Comparison Algorithm

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then
    1. If Type(x) is Undefined, return true.
    2. If Type(x) is Null, return true.
    3. If Type(x) is Number, then
      1. If x is NaN, return false.
      2. If y is NaN, return false.
      3. If x is the same Number value as y, return true.
      4. If x is +0 and y is −0, return true.
      5. If x is −0 and y is +0, return true.
      6. Return false.
    4. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
    5. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
    6. Return true if x and y refer to the same object. Otherwise, return false.
  2. [...]

11.9.6 The Strict Equality Comparison Algorithm

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same Number value as y, return true.
    4. If x is +0 and y is −0, return true.
    5. If x is −0 and y is +0, return true.
    6. Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false.

Therefore,

function Person(name) {
  this.name = name;
}
Person.prototype.kind = 'person';
var zack = new Person('Zack');
zack.__proto__ == Person.prototype;  // true
zack.__proto__ === Person.prototype; // true

If you say it returns false in Eclipse IDE, it means Eclipse IDE doesn't follow ECMAScript standard.

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

1 Comment

When you said true reflects in both situations, I then cross checked with Google Chrome. And thank you very much.

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.