Find 'a' if this equality is true! (a==1 && a==2 && a==3)

revanthrev23

revanthrev23

Posted on June 4, 2021

Find 'a' if this equality is true! (a==1 && a==2 && a==3)

Yes I am not kidding!

Yes there really is an answer for this!

JavaScript is one of the most daunting programming languages that one can ever come across. But yet it's so beautiful. It's so complex, but stick by it, it will turn out to be one of the best choices you ever made.

One of the most feared topics in JavaScript is functions. The true power of JavaScript lies in its functions. We are gonna be using functions(well technically one function) to get an answer for this question.

Enough blah blah, lets get to the main act:

I am assuming that you know functions, objects, and concept of anonymous functions as a prerequisite. If not I suggest, do a quick read about it and get back here to get your minds blown at the simplicity of the solution.

We use '==' to check equality in some of the languages like Java or C++. In JavaScript we can use the same, but we tend to use '==='. The reason for this is that, '==' performs a loose equality operation where type is not checked. TypeCoercion will happen, where an operand on one side of the '==' is converted to the type of the other, and then its values are compared. If the values are same, then 'true' is returned. Else a 'false' will be returned. Simple?

Now this is where we can make use of this 'loose equality operator' to our advantage.

Now let us think what datatype would our variable 'a' be? Is it a number? Is it a character? Is it a string? Is it a function(yes it is allowed in JS)? Or is it an object?

Let's find out in the code:

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

console.log(equality);
Enter fullscreen mode Exit fullscreen mode

This code would print 'true'. Try it out!

Now let's go through what the code is actually doing. We are declaring an object of the name 'a'. It has a member variable 'num' which is initialised to 0. We also have another member named 'valueOf' which is a function.

Notice the value of 'valueOf' (pun intended!). It is a weird way to declare a function right? Well yes, for starters, it does not have a name! This is known as an anonymous function.

Let us continue, so we have a weird function, where in we are just incrementing our 'num' by 1 and returning the updated value. So this was our object. Simple?

Now we have the heading of this blog in the next line:

const equality = (a==1 && a==2 && a==3);

Let's break it down to parts. In the first part we are using our '==' to check if our object 'a' is equal to 1(Integer). We will get a false right? Phew, EZ! But sadly that's not what happens. This is where the magic of JavaScript happens, because we are using '==' it converts our object into an Integer, just the way I told earlier.

Ok so we have 'a' which is now converted to an Integer. But what is its value? JavaScript converts 'a' into an Integer and uses the inbuilt function 'valueOf()' to obtain this value of our object. But wait, we already have a function named 'valueOf' defined in our object, remember?

So JavaScript does a.valueOf(), hence it is calling the function named 'valueOf' which is defined in our object 'a'.

How do we call a function defined in object?
By using this syntax right?

object_name.function_name();
Enter fullscreen mode Exit fullscreen mode

Hence our 'valueOf' function is called and it returns the current value of num+1. Initially num is 0, so we get 1 returned. Now we have 1==1 which is true.

The same happens for the other two parts as well, and we get true as the value of our const 'equality'.

Go on check the console, what are you waiting for?

💖 💪 🙅 🚩
revanthrev23
revanthrev23

Posted on June 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related