a == b or 1 == 2 are false but in the code below, the output is true. How could that have been possible?

const a = { num: 0, valueOf: function() { return this.num += 1; } }; const equality = (a==1 && a==2); console.log(eqaulity);

6/26/2019 6:07:12 PM


14 Answers

New Answer


Mofey Here's the explanation... 1. The expressions are comparing an object `a` with numbers 1 and 2 in both instances. So... these comparisons involve different types. 2. The double equal sign (==) is a loose equality meaning it will first convert the operands into a common primitive type between the two, in this case, a Number. 3. Type coercion for the object will involve calling the `valueOf()` method first, which has been declared in the object definition. The first reference to (a==1 && ... ) invokes a call to `a.valueOf()` which increments `a.num` from 0 to 1. The 2nd reference ( ... && a==2) invokes the same method, incrementing `a.num` and returning 2. Therefore, the equality check above essentially becomes: (1==1 && 2==2) Both evaluate to true. Here's are some articles I found to help you grasp this more clearly: http://2ality.com/2012/11/coercing-objects.html https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a https://www.valentinog.com/blog/coercion/


Santanu Sikder 👍You are welcome!😊 David Carroll solved this,😉 I tried to give an acceptable answer.🍻


Santanu Sikder When we assign an object to a variable, the reference of the object is what the variable holds and not the object itself. So, when assigning an object to a constant variable, the reference of the object becomes constant to that variable and not to the object itself. Therefore, the object is mutable.


Mofey I hope this example can help you👍 https://code.sololearn.com/W3CwqAGgJqRV/?ref=app


Danijel Ivanović to be honest, it really didn't


Wow, David Carroll, that's a really huge explanation, comparable with the lecture given in the University. However though, it helped. Thank you


Good question Santanu Sikder, I'd leave that for my bosses in the room David Carroll, Sanjay Kamath, Danijel Ivanović, Diego to answer for you 🙂


Santanu Sikder `const` is applied to the object, not the number itself. This doesn't make the object immutable. It only makes it such that the reference to the object for the symbol `a` cannot be changed. That said, I find it whacked that the equality check is being made between an object and two numbers. 😂🤣 I mean... who does that? 🤦‍♂️🤷‍♂️


According to me, each time the object a is appearing in the equality check, its member function valueOf() is also run. And everytime it is run, it does this.num += 1 and returns that value. So, initially num = 0; then on the first call to a (in the equality check), this happens: num = 0; num += 1; // = 1, by the valueOf() function return num; // Returns 1 and, a == 1 returns true. Then, on the second call to a (in the equality check), this happens: num = 1; // After the previous increment by valueOf() num += 1; // = 2, by the valueOf() function return num; // Returns 2 and this time, a == 2 returns true. Since both of them return true, && operator returns true and hence, the check returns true. But one question- What is the use of the const keyword here, if num isn't constant?


@ Diego hmm 🤔


Thanks David Carroll and Danijel Ivanović for solving my confusion. Thanks for giving such a good explanation 👍


I think that they are taken as strings which makes them equal with each others P.S i'm new to this so don't take it seriosly bcs it's just my begginer's idea


a is always 1 and constant by definition 🤔 till increment makes it 2 T && T = 1