a == b or 1 == 2 are false but in the code below, the output is true. How could that have been possible? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 26

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);

26th Jun 2019, 6:07 PM
eMBee
eMBee - avatar
14 Answers
+ 36
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/
27th Jun 2019, 12:47 AM
David Carroll
David Carroll - avatar
+ 20
Santanu Sikder 👍You are welcome!😊 David Carroll solved this,😉 I tried to give an acceptable answer.🍻
28th Jun 2019, 12:07 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 18
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.
28th Jun 2019, 9:37 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 13
Mofey I hope this example can help you👍 https://code.sololearn.com/W3CwqAGgJqRV/?ref=app
26th Jun 2019, 9:39 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
Danijel Ivanović to be honest, it really didn't
27th Jun 2019, 12:06 AM
eMBee
eMBee - avatar
+ 9
Wow, David Carroll, that's a really huge explanation, comparable with the lecture given in the University. However though, it helped. Thank you
27th Jun 2019, 1:24 AM
eMBee
eMBee - avatar
+ 9
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?
28th Jun 2019, 5:30 AM
SSki11
SSki11 - avatar
+ 9
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 🙂
28th Jun 2019, 7:00 AM
eMBee
eMBee - avatar
+ 9
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? 🤦‍♂️🤷‍♂️
28th Jun 2019, 7:34 AM
David Carroll
David Carroll - avatar
+ 7
Thanks David Carroll and Danijel Ivanović for solving my confusion. Thanks for giving such a good explanation 👍
28th Jun 2019, 11:41 AM
SSki11
SSki11 - avatar
+ 6
@ Diego hmm 🤔
26th Jun 2019, 6:45 PM
eMBee
eMBee - avatar
+ 3
a is always 1 and constant by definition 🤔 till increment makes it 2 T && T = 1
27th Jun 2019, 9:16 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
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
28th Jun 2019, 10:40 AM
Juri M
0
Hi guys
28th Jun 2019, 1:02 PM
NESHA VENKATA RAJU