user2132404
user2132404

Reputation: 35

Chrome console.log contradictions

I have a simple class

function TrueNinja() {
   this.vanish = function() { return this; };
}

creating a new object from this

var someNinja = new TrueNinja();

when i do the following in chrome i get two different outputs

console.log(someNinja instanceof TrueNinja); // i get true
console.log("someNinja: " + someNinja instanceof TrueNinja); //i get false

WHY?? :-( the first statement is the correct output since someNinja IS an instance of TrueNinja... but why do i get false in the next statement?

Upvotes: 0

Views: 53

Answers (2)

moonwave99
moonwave99

Reputation: 22820

That's due to operator precedence, + is evaluated before instanceof.

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167240

You need to give like this:

"someNinja: " + (someNinja instanceof TrueNinja);

The reason is, the whole "someNinja: " + someNinja is compared with TrueNinja.

So, this way, it compares, "someNinja: " + someNinja With the TrueNinja, where, the former is string and latter is TrueNinja. Hope it is clear! :)

The actual reason is because of Operator Precedence, where + comes before instanceof.

Upvotes: 1

Related Questions