JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – compare the self value with data type

JavaScript Quiz – compare the self value with data type

Compare Value With Data Type –

In javascript, compare with data type is interesting as there are two types of values which are primitive and non-primitive values but there multiple type of data type. When compared to the self-value with the data type.

As compare with data type not only compare with the value itself but also compare with data type also. Like if you compare “5” and 5, they look equal but both types are not equal.

“5” === 5 => Not Equal

As the “5” data type is String and the other one 5 data type is Number. That’s why both of them not equal.

JavaScript Question

In this javascript quiz, let’s find put can compare the self value with data type in javascript leads to different different values.

First, declare arrow function with name compare using const keyword and it accept one parameter.

Inside the function, parameter compare with itself with data type match.

const compare = b => b === b;

Here b is the function parameter which is passed in function and in return this function return the result of comparison.

Finally, we test this function with 4 different value which are as follows.

  1. console.log(compare(null));
  2. console.log(compare(undefined));

Now, compare with NaN, which is Not a Number.

  1. console.log(compare(NaN));
  2. console.log(compare([NaN]));

Now, any guess what should be the output of all above statements.

Answer – true, true, false, true

Click To Run Code

Explanation

Now, you know the answer is true, true, false, true. But why all value is true except the one which NaN value. Let me explain.

First, we compare the null with data type as it return true because null is nothing but internal pointer with zero. So it is like comparing two references having value zero.

And the same case happen with undefined also.

compare(null); = true

compare(undefined); = true

Compare Not A Number (NaN) –

Now, in the next step, we compare a NaN and NaN in the array. So, NaN when compare with NaN is not equal that’s why it not return true.

For more detail about NaN is not equal to NaN!

But for NaN in array is true because during comparison it implicit converted to string.

compare(NaN); = false

compare([NaN]); = true

At last, That’s why we get an answer “truetrue, false, true“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – can string split method will be limited
  2. JS Quiz – can array from method be like a map method
  3. JavaScript Quiz – Infinity number is a true or false value
  4. JavaScript Quiz – array single value comparison can be possible

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link